完整的程序如下:
void get_and_print(void)
{
char buffer[16];
scanf("%s",buffer);
printf(buffer);
}
int main()
{
printf("Give me some input and I will echo it back:\n");
get_and_print();
printf("\nOK,I'm done!\n");
exit(0);
}
但有两个漏洞,如何改正呢?
void get_and_print(void)
{
char buffer[16];
scanf("%s",buffer);
printf("%s\n",buffer); /*在这里("%s\n",buffer)因为buffer是一个地址*/
}
int main()
{
printf("Give me some input and I will echo it back:\n");
get_and_print();
printf("\nOK,I'm done!\n");
exit(0);
What's the problem? I think your program will run. But there is one thing: when you are using scanf() to read a string, it will stop after it meets the first white space which means it won't read whatever after that white space. I suggest you use get() function to read a string.
For example, if you input "Hello, world!", you will only get "Hello" back when you are using scanf() function.