Ç - 警告:格式'%s'的预期中键入'字符*',但参数2的类型'字符(*)[20]“ [英] C - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’
问题描述
我想运行一个简单的C程序,但我得到这个错误:警告:格式'%s'的预期中键入'字符的,但参数2的类型'字符(的)[20 ]'
在运行Mac OSX山狮
使用gcc 4.2.1编译终端
的#include<&stdio.h中GT;诠释的main()
{
烧焦我[20]; 的printf(你叫什么名字?);
scanf函数(%S,&安培;我);
的printf(混账很高兴见到你,%S \\ n!,我); 返回(0);
}
scanf函数(%S,&安培;我);
应
scanf函数(%S,我的);
释解:
%S
表示 scanf函数
期待一个指向一个字符数组的第一个元素。 我
是一个对象数组,并可能为指针评估。所以这就是为什么你可以使用我
直接无需添加&安培;
。添加&安培;
到我
将进行评估,以<code>'字符(*)[20] 和你scanf的等待的char *
code评论家:
使用%S
可能会导致缓冲区溢出,如果用户输入的字符串长度> 20.因此,将其更改为%19S
:
scanf函数(%19秒,我的);
I am trying to run a simple C program but am getting this error: " warning: format ‘%s’ expects type ‘char ’, but argument 2 has type ‘char ()[20]’"
Running Mac OSX Mountain Lion Compiling in terminal using gcc 4.2.1
#include <stdio.h>
int main()
{
char me[20];
printf("What is your name?");
scanf("%s",&me);
printf("Darn glad to meet you, %s!\n",me);
return(0);
}
scanf("%s",&me);
should be
scanf("%s",me);
Explaination:
"%s"
means that scanf
is expecting a pointer to the first element of a char array. me
is an object array and could evaluated as pointer. So that's why you can use me
directly without adding &
. Adding &
to me
will be evaluated to ‘char (*)[20]’
and your scanf is waiting char *
Code critic:
Using "%s"
could cause a buffer overflow if the user input string with length > 20. So change it to "%19s"
:
scanf("%19s",me);
这篇关于Ç - 警告:格式'%s'的预期中键入'字符*',但参数2的类型'字符(*)[20]“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!