为什么我会得到这个code例子警告?什么是正确的? [英] Why am I getting warnings about this code example? What is proper?
本文介绍了为什么我会得到这个code例子警告?什么是正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我学习一些C,并从的 scanf函数阅读了>其中包含下列code座本教程:
I'm learning some C, and was reading up on scanf
from this tutorial where the following code block is included:
#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);
}
不过,我得到的警告:
However I get warnings that:
"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);
}
这篇关于为什么我会得到这个code例子警告?什么是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文