I was wondering why realloc for a char* or whatever 1D array works if I do something like oldpointer=realloc(oldpointer,newsize);
But when I try with a 2D char* array it fails. Looking here and there I see that sometimes people use NewPointer=realloc(oldpointer,newsize) but if it's the only use it will not be useful to me, since I need to resize the matrix' columns and rows often, in a loop (I must fill an array of strings without knowing first how many string I will insert nor the size of each one)
the code I used for trying is this,
void main(){
int max = 5, i,j;
char **matrix;
matrix=malloc(max*sizeof(char));
for(i=0;i<max;i++){
matrix[i]=malloc(max*sizeof(char));
}
matrix=realloc(matrix,max*2*sizeof(char));
strcpy(matrix[4],"we\0");
printf("%s",matrix[4]);
}
Error in `./out': realloc(): invalid next size: 0x00000000015c4010 *** Aborted