Code:
a=malloc((CharCount+1) * sizeof(a)); // 1 added to prevent 0 bytes being allocated
That's wrong. You are getting the size of a pointer, not the size of a character (which is always 1 anyway).
Code:
while ((c=getchar()) != EOF)
{
*a=realloc(NULL, CharCount * sizeof(a));
The first argument is wrong, the assignment is wrong, and you shouldn't directly assign it anyway:
Code:
while( ... )
{
char *t = realloc( a, CharCount + 1 );
if( t != NULL )
a = t;
else
outofmemoryoops();
a[ CharCount++ ] = c;
}
Now then, if you want it to be a string, you need to have allowed for a nul character at the end, and you need to keep assigning it to the newest spot allocated, after you overwrite the old one with the new character. There's all sorts of fun ways to do that:
Code:
char in[2] = { 0 };
while( (in[0]=getchar()) != EOF )
{
...
strcat( a, in );
Quzah.