Possible Duplicate:
Realloc is not resizing array of pointers
Can anyone tell me where my mistake is? this function should make an array of the characters supplied from stdin
. I read some related questions but it was too complicated for me to understand.
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;
}
Output:
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