I'm trying to write and read a char
three dimensional array, or said in other words an array of strings arrays.
I've already try to allocate the memory in each part of the array, but it keeps failing with the following error: Segment fault (core dumped)
/**
* Create array to write
*/
char writed[2][2][512] = {{"Hello", "World"}, {"Bye", "World"}};
/**
* Allocate memory
*/
char *** readed = (char ***)malloc(sizeof(char **) *2 * 2 * 512);
for (int z = 0; z < 2; z++) {
readed[z] = (char **)malloc(sizeof(char **) * 2 * 512 );
for (int y = 0; y < 2; y++) {
readed[z][y] = (char *)malloc(sizeof(char) * 512);
}
}
/**
* Write array
*/
FILE *writeFile = fopen("strings", "wb");
fwrite(writed, 2 * 2 * 512, 2 * 512, writeFile);
fclose(writeFile);
/**
* Read array
*/
FILE *readFile = fopen("strings", "rb");
fread(readed, 2 * 2 * 512, 2 * 512, readFile);
fclose(readFile);