我正在为我的C课程工作,我正在尝试接受用户的输入并将其存储在一个变量中,以便稍后在我的代码中使用.这是我的主要功能,
int main() {
// Variables here
char* inputLine[10];
do {
printf("Insert number....");
scanf("%s\n", inputLine);
// More stuff here
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码给了我一堆警告,warning: format specifies type 'char *' but the argument has type 'char **' [-Wformat]如果我将变量声明更改为,
char* inputLine = NULL;
Run Code Online (Sandbox Code Playgroud)
当我执行我的代码时,我得到一个段错误,有人可以向我解释我做错了什么,以及当我初始化这个变量时内存中发生的事情的差异?
char* inputLine[10];
- >是十个指针的数组 char
printf的格式%s需要类型的参数char *,但是你将它作为类型提供char **
只是用
char inputLine[10];
为避免可能的缓冲区溢出,您应该使用
scanf("%9s", inputLine); //Notice the size with %s
9只是因为C字符串是null terminate('\0')所以它的一个额外字节结束
| 归档时间: |
|
| 查看次数: |
922 次 |
| 最近记录: |