为什么我会收到有关此代码示例的警告? 什么是正确的?

我正在学习一些C,并且正在阅读本教程中的scanf ,其中包含以下代码块:

 #include  int main() { char str1[20], str2[30]; printf("Enter name: "); scanf("%s", &str1); printf("Enter your website name: "); scanf("%s", &str2); printf("Entered Name: %s\n", str1); printf("Entered Website:%s", str2); return(0); } 

但是我收到警告:

 "Format specifies type 'char *' but the argument has type 'char (*)[20]' 

教程错了吗?

这应该适合你:

 #include  int main() { char str1[20], str2[30]; printf("Enter name: "); scanf("%19s", str1); //^^ ^ Removed address operator //So only the right amount of characters gets read printf("Enter your website name: "); scanf(" %29s", str2); //^ Added space to catch line breaks from the buffer printf("Entered Name: %s\n", str1); printf("Entered Website:%s", str2); return(0); } 

本教程的示例中存在一个错误。

更改:

 scanf("%s", &str1); 

 scanf("%s", str1); 

s转换说明符需要指向char的指针,但是您正在传递指向数组的指针。

线条

 scanf("%s", &str1); 

 scanf("%s", &str2); 

确实是错的(至少,它们都包含一个错字)。 它们应该写成

 scanf("%s", str1); // no & operator 

 scanf("%s", str2); // ditto 

数组和数组表达式在C中是特殊的。除非它是sizeof或一元&运算符的操作数,或者是用于在声明中初始化另一个数组的字符串文字,否则类型为“N元素数组T ”的表达式将被转换(衰减)为“指向T指针”的表达式,表达式的值将是数组的第一个元素的地址。

表达式 str1具有类型“20个元素的char数组”。 如果str1出现在不是sizeof或一元&运算符的操作数的上下文中,它将被转换为“指向char指针”类型的表达式,并且表达式的值将与&str1[0] ; 这就是为什么你不需要使用&来读取字符串,因为数组表达式将被视为指针。 但是,当它是一元&运算符的操作数时,转换规则不适用,并且表达式&str1的类型是“指向20个元素的char数组的指针”( char (*)[20] )。 因此你的警告。

str1&str1相同(第一个元素数组的地址与数组的地址相同),但表达式的类型不同,类型也很重要。 指向char的指针将与指向char数组的指针区别对待。

90%的C书和教程都是废话 ; 对任何不是实际标准的C引用持怀疑态度。 Harbison&Steele的C:A参考手册 (目前是第5版)自80年代末以来一直是我的参考,但即使它有小错误。