I have the following C code:
char buffer[255];
char ***data = NULL;
int i = 0;
int size = 10;
**data = (char*) calloc(size, sizeof(char**));
while(fgets(buffer, 255, database)) {
if(i + 1 >= size) {
size += 10;
**data = (char*) realloc(**data, size*sizeof(char**));
}
char **line = str_split(buffer, ',', 6);
**data[i++] = **line;
}
I am a noob at C. And I do not rearly understand my code. But I want a 3D char array (in Java I woud say a 2D String array). The first [] are the lines from my textfile reading with 'fgets' from 'database' (and I don't know how many lines, thats why I want it dynamic). The second [] and thirt [] are filled with the values from str_split.
str_split works fine, thats the code:
char** str_split(char* str, const char a_delim, int count) {
char **result;
char *token;
char *rest = str;
char delim[] = { a_delim, '\0' };
result = (char**) malloc(sizeof(char*) * count);
int i = 0;
while((token = strtok_s(rest, delim, &rest))) {
*(result + i++) = token;
}
return result;
}
So the problem is at run time I get the following error:
Access violation writing location 0x00000000.
What is wrong?
**data = (char*) calloc(size, sizeof(char**));-->data = (char***) calloc(size, sizeof(char**));