<分区>
Possible Duplicate:
Realloc is not resizing array of pointers
谁能告诉我我的错误在哪里?此函数应生成一个由 stdin
提供的字符组成的数组。我看了一些相关的问题,但太复杂了,我看不懂。
char *readChar(void)
{
int c;
int len = 0;
char* parr = malloc(sizeof(char));
while((c = getchar()) != EOF)
{
++len;
int size = sizeof(char)*len;
parr = (char *) realloc(parr,size);
*(parr+size-1) = (char) c;
printf("Done! Size should be: %dB, but real size is %dB\n",size,sizeof(parr));
}
return parr;
}
输出:
Done! Size should be: 1B, but real size is 8B Done! Size should be: 2B, but real size is 8B Done! Size should be: 3B, but real size is 8B Done! Size should be: 4B, but real size is 8B