为什么我会收到有关此代码示例的警告?什么是适当的?
Why am I getting warnings about this code example? What is proper?
我正在学习一些 C,并且正在阅读 this tutorial 的 scanf
,其中包含以下代码块:
#include <stdio.h>
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]'
教程有错吗?
本教程的示例中存在错误。
变化:
scanf("%s", &str1);
到
scanf("%s", str1);
s
转换说明符需要指向 char
的指针,但您传递的是指向数组的指针。
这应该适合你:
#include <stdio.h>
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", &str2);
确实是错误的(至少,它们都包含一个错字)。他们应该写成
scanf("%s", str1); // no & operator
和
scanf("%s", str2); // ditto
数组和数组表达式在 C 中是特殊的。除非它是 sizeof 或一元 &
运算符的操作数,或者是用于在声明中初始化另一个数组的字符串文字,[=类型"N-element array of T
"的50=]表达式将被转换(衰减)为类型"pointer to T
"的表达式,表达式的值将是数组第一个元素的地址.
表达式 str1
的类型为“char
的 20 元素数组”。如果 str1
出现在不是 sizeof
或一元 &
运算符的操作数的上下文中,它将被转换为类型 "pointer to char
" 的表达式,并且值表达式将与 &str1[0]
相同;这就是为什么您不需要使用 &
来读取字符串的原因,因为数组表达式将被视为指针。但是,当它是一元运算符&
的操作数时,转换规则不适用,表达式&str1
的类型是"pointer to 20-element array of char
"(char (*)[20]
)。因此你的警告。
str1
和&str1
的值会相同(首元素数组地址与数组地址相同), 但表达式的类型不同,类型很重要。指向 char
的指针与指向 char
数组的指针的处理方式不同。
90% 的 C 书籍和教程都是废话;非常怀疑任何不是实际标准的 C 参考。 Harbison & Steele 的 C: A Reference Manual(目前第 5 版)自 80 年代后期以来一直是我的首选参考,但即使它也有小错误。
我正在学习一些 C,并且正在阅读 this tutorial 的 scanf
,其中包含以下代码块:
#include <stdio.h>
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]'
教程有错吗?
本教程的示例中存在错误。
变化:
scanf("%s", &str1);
到
scanf("%s", str1);
s
转换说明符需要指向 char
的指针,但您传递的是指向数组的指针。
这应该适合你:
#include <stdio.h>
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", &str2);
确实是错误的(至少,它们都包含一个错字)。他们应该写成
scanf("%s", str1); // no & operator
和
scanf("%s", str2); // ditto
数组和数组表达式在 C 中是特殊的。除非它是 sizeof 或一元 &
运算符的操作数,或者是用于在声明中初始化另一个数组的字符串文字,[=类型"N-element array of T
"的50=]表达式将被转换(衰减)为类型"pointer to T
"的表达式,表达式的值将是数组第一个元素的地址.
表达式 str1
的类型为“char
的 20 元素数组”。如果 str1
出现在不是 sizeof
或一元 &
运算符的操作数的上下文中,它将被转换为类型 "pointer to char
" 的表达式,并且值表达式将与 &str1[0]
相同;这就是为什么您不需要使用 &
来读取字符串的原因,因为数组表达式将被视为指针。但是,当它是一元运算符&
的操作数时,转换规则不适用,表达式&str1
的类型是"pointer to 20-element array of char
"(char (*)[20]
)。因此你的警告。
str1
和&str1
的值会相同(首元素数组地址与数组地址相同), 但表达式的类型不同,类型很重要。指向 char
的指针与指向 char
数组的指针的处理方式不同。
90% 的 C 书籍和教程都是废话;非常怀疑任何不是实际标准的 C 参考。 Harbison & Steele 的 C: A Reference Manual(目前第 5 版)自 80 年代后期以来一直是我的首选参考,但即使它也有小错误。