2-Dimensional character strings and realloc()
Hello, the following code fails/crashes in the second loop where it prints the content and frees the memory. But the issue could be with the first loop. Any help is appreciated in advance.
Code:
int main(int argc, char *argv[])
{
char **a, buf[80];
int x;
a = (char **)realloc(NULL, sizeof(char *));
for(x = 0; x < 5; x++)
{
gets(buf);
a[x] = (char *)realloc(a, strlen(buf) * sizeof(char) + 1);
}
printf("--------------\n");
for(x = 0; x < 5; x ++)
{
printf("%s\n", a[x]);
free(a[x]);
}
free(a);
return 0;
}