我正在学习一些 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]'
教程有错吗?
最佳答案
这应该适合你:
#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);
}
关于c - 为什么我会收到有关此代码示例的警告?什么是适当的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27805793/