I want to allocate my 2D char array dynamically. Here I try to do this in cycle, but it gets me segmentation fault error. If I remove "arr[length] = str;" it will be ok. But it's not what I want to do.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char** arr;
int i = 0;
int length = 0;
for(i = 0; i < 4; i ++) {
arr = (char**) realloc(arr, (length+1) * sizeof(char*));
char* str = (char*) malloc(4 * sizeof(char));
arr[length] = str;
}
return 0;
}