C语言 C - 警告:格式“%s”需要类型“char *”,但参数 2 的类型为“char (*)[20]”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16570716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:20:40  来源:igfitidea点击:

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

carrayspointers

提问by l00kitsjake

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);
}

回答by MOHAMED

scanf("%s",&me);

should be

应该

scanf("%s",me);

Explaination:

说明:

"%s"means that scanfis expecting a pointer to the first element of a char array. meis an object array and could evaluated as pointer. So that's why you can use medirectly without adding &. Adding &to mewill be evaluated to ‘char (*)[20]'and your scanf is waiting char *

"%s"意味着它scanf需要一个指向 char 数组第一个元素的指针。me是一个对象数组,可以评估为指针。所以这就是为什么您可以me直接使用而无需添加&. 添加&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":

"%s"如果用户输入长度大于 20 的字符串,使用可能会导致缓冲区溢出。因此将其更改为"%19s"

scanf("%19s",me);

回答by John Bode

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或一元&运算符,或者是字面被用于初始化的声明的阵列,类型“的N元件阵列的表达式的字符串T将被转换(“衰变”)”到“指向T”类型的表达式,它将计算数组中第一个元素的地址。

The array meis declared as a 20-element array of char; normally, when the expressionmeappears in your code, it will be treated as an expression of type "pointer to char". If you had written

该数组me被声明为 20 个元素的数组char;通常,当表达式me出现在您的代码中时,它将被视为“指向char”类型的表达式。如果你写过

scanf("%s", me);

then you wouldn't have gotten the error; the expression mewould 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 arrayof char(char (*)[20]), which is not what scanfexpects for the %sconversion specifier, hence the diagnostic.

&但是,通过使用运算符,您绕过了该规则;char您传递的不是指向 的指针,而是指向( )数组的指针,这不是转换说明符所期望的,因此是诊断。charchar (*)[20]scanf%s

回答by Truly Amazing Videos By Ravi A

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)。