我精通几种脚本语言,但我最终还是强迫自己学习原始的C语言。我只是在玩弄一些基本的东西(现在的I/O)。如何分配堆内存,将字符串存储在已分配的内存中,然后再将其输出?这就是我现在所拥有的,我怎样才能让它正常工作呢?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *toParseStr = (char*)malloc(10);
scanf("Enter a string",&toParseStr);
printf("%s",toParseStr);
return 0;
}
目前我得到了像'8'\‘这样奇怪的输出。
发布于 2010-07-21 10:42:19
使用带有标准"%s“说明符的scanf()
(或对您不能控制的数据使用fscanf()
)几乎肯定会使您陷入缓冲区溢出的麻烦。
一个经典的例子是,如果我在你的程序中输入字符串“这个字符串远远超过10个字符”,混乱就会随之而来,猫和狗将开始睡在一起,一个赤裸裸的奇点很可能会出现并吞噬地球(大多数人只是说“未定义的行为”,但我认为我的描述更好)。
我积极地阻止使用不能提供保护的功能。我强烈建议您(特别是作为C语言的新手)使用fgets()
来读取您的输入,因为使用它可以更容易地控制缓冲区溢出,而且它比scanf()
更适合于简单的行输入。
一旦有了一行代码,您就可以随意地对它调用sscanf()
,顺便说一句,在这种特殊情况下,您不需要这样做,因为您只获得了一个原始字符串。
我将使用:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSZ 10
int main(int argc, char *argv[]) {
char *toParseStr = malloc(BUFFSZ+2);
if (toParseStr == NULL) {
printf ("Could not allocate memory!\n");
return 1;
}
printf ("Enter a string: ");
if (fgets (toParseStr, BUFFSZ+2, stdin) == NULL) {
printf ("\nGot end of file!\n");
return 1;
}
printf("Your string was: %s",toParseStr);
if (toParseStr[strlen (toParseStr) - 1] != '\n') {
printf ("\nIn addition, your string was too long!\n");
}
free (toParseStr);
return 0;
}
发布于 2010-07-21 10:33:59
在scanf
中,在toParseStr
之前不需要&
,因为它已经是一个指针
之后还要调用free(toParseStr)
发布于 2010-07-21 10:40:49
首先,阻碍您的程序工作的错误:scanf(3)
接受格式字符串,就像printf(3)
一样,而不是要为用户打印的字符串。其次,传递的是指针toParseStr
的地址,而不是指针toParseStr
的地址。
我还从您对malloc(3)
的调用中删除了不必要的强制转换。
您的程序仍然需要改进的一点是,使用scanf(3)
的a
选项为您分配内存--这样一些恶作剧者将10个字符放入您的字符串中时,就不会开始践踏不相关的内存。(是的,C语言会让人按照编写的那样覆盖几乎整个地址空间。巨大的安全缺陷。:)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *toParseStr = malloc(10);
printf("Enter a short string: ");
scanf("%s",toParseStr);
printf("%s\n",toParseStr);
return 0;
}
https://stackoverflow.com/questions/3295884
复制相似问题