简体   繁体   English

警告:格式“%s”需要类型“char *”,但参数 2 的类型为“char (*)”

[英]warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)’

I am trying to run a simple C program but I am getting this error:我正在尝试运行一个简单的 C 程序,但出现此错误:

warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[20]'警告:格式“%s”需要类型“char *”,但参数 2 的类型为“char (*)[20]”

Running Mac OSX Mountain Lion, compiling in terminal using gcc 4.2.1运行 Mac OSX Mountain Lion,使用 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. "%s"意味着scanf需要一个指向 char 数组第一个元素的指针。 me is an object array and could evaluated as pointer. me是一个对象数组,可以评估为指针。 So that's why you can use me directly without adding & .所以这就是为什么你可以直接使用me而无需添加& Adding & to me will be evaluated to 'char (*)[20]' and your scanf is waiting char *me添加&将被评估为'char (*)[20]'并且您的 scanf 正在等待char *

Code critic:代码评论家:

Using "%s" could cause a buffer overflow if the user input string with length > 20. So change it to "%19s" :如果用户输入的字符串长度大于 20,则使用"%s"可能会导致缓冲区溢出。因此将其更改为"%19s"

scanf("%19s",me);

Except when it is the operand of the sizeof , _Alignof , or unary & operators, or is a string literal being used to initialize an array in a declaration, an expression of type "N-element array of T " will be converted ("decay") to an expression of type "pointer to T ", and it will evaluate to the address of the first element in the array.除非它是sizeof_Alignof或一元&运算符的操作数,或者是用于在声明中初始化数组的字符串文字,否则类型为“ T N 元素数组”的表达式将被转换(“衰减") 到一个“指向T指针”类型的表达式,它将计算数组中第一个元素的地址。

The array me is declared as a 20-element array of char ;数组me被声明为一个 20 元素的char数组; normally, when the expression me appears in your code, it will be treated as an expression of type "pointer to char ".通常,当表达式me出现在您的代码中时,它将被视为“指向char指针”类型的表达式。 If you had written如果你写过

scanf("%s", me);

then you wouldn't have gotten the error;那么你就不会得到这个错误; the expression me would have been converted to an expression of the correct type.表达式me将被转换为正确类型的表达式。

By using the & operator, however, you've bypassed that rule;但是,通过使用&运算符,您绕过了该规则; instead of a pointer to char , you're passing a pointer to an array of char ( char (*)[20] ), which is not what scanf expects for the %s conversion specifier, hence the diagnostic.代替指针char ,你传递一个指针数组charchar (*)[20]这是不什么scanf为预计%s转换指定,因此诊断。

Another way you could fix this issue is by doing this:解决此问题的另一种方法是执行以下操作:

scanf("%s",&me[0]);

You actually have to give the array's starting point (which in most languages is 0).您实际上必须给出数组的起点(在大多数语言中为 0)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 格式&#39;%s&#39;期望参数类型为&#39;* char&#39;,但参数2的类型为&#39;int&#39; - format '%s' expects argument of type '*char' but argument 2 has type 'int' 格式 &#39;%c&#39; 需要类型为 &#39;char *&#39; 的参数,但参数 2 的类型为 &#39;char (*)[0]&#39; - format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[0]’ s期望使用char c类型的参数,但是参数2具有类型&#39;int&#39;的警告和错误的返回值 - s expects argument of type char c but argument 2 has type 'int' warning and bad return 数组和“警告:格式&#39;%s&#39;需要匹配的&#39;char *&#39;参数[-Wformat =]” - Array and “warning: format '%s' expects a matching 'char *' argument [-Wformat=]” 警告:格式&#39;%lu&#39;期望参数类型为&#39;long unsigned int&#39;,但是参数4的类型类型为&#39;long unsigned int *&#39;[-Wformat] - warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'long unsigned int *' [-Wformat] C 语言:收到错误:警告:格式 &#39;%d&#39; 需要类型为 &#39;int *&#39; 的参数,但参数 2 的类型为 &#39;int **&#39; [-Wformat=] - C language: received error: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat=] 如何解决“格式指定类型为&#39;char *&#39;但参数具有类型为&#39;char **&#39;”的错误 - How to fix “format specifies type 'char *' but the argument has type 'char **'” error C:警告:数组初始值设定项中的元素过多; &#39;xxx&#39; 即将初始化; 需要“char *”,但类型为“int” - C: warning: excess elements in array initializer; near initialization for ‘xxx' ; expects ‘char *’, but has type ‘int’ 带有char数组的C警告:“警告:数组下标的类型为&#39;char&#39;” - C warning with char array: “warning: array subscript has type 'char'” 期望&#39;const char *&#39;但参数在C中的类型为&#39;char **&#39; - expected ‘const char *’ but argument is of type ‘char **’ in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM