I have 2D char array, dynamically allocated (using malloc). I need to add another line to it but I have no idea how to use realloc with 2D arrays.
I have this:
res = (char **) malloc(1 * sizeof (char*));
for (i = 0; i < 1; i++) {
res[i] = (char *) malloc(z * sizeof (char));
}
1xz array (one line with z elements)
Now I need to change it to 2xz.
res=(char **) realloc(res, sizeof(*res)*count);
for(k=0;k<count;k++) res[k]=(char *) realloc(res[i],sizeof(*res[i])*z);
for(k=pocet;k<count+1;k++) res[k]=(char *) malloc(sizeof(*res[i])*z);
I was told to reallocate what was already allocated using realloc and then allocate new part using malloc. But it's not working and program crashes. Can anyone give me a hint? :)