Why this (dynamic array) doesn't crash ?
Hi, i have the following code which manages to get words from a line and store them into an array (number of words is unknown).
Code:
char **createArray(char *line, int *length){
char **array = (char **)malloc(sizeof(char));
char *word = (char *)malloc(sizeof(char));
if (word==NULL)
printf("fail allocating memory");
word = strtok(line, " ");
int i=0;
while (word!=NULL){
array[i] = (char *)malloc(sizeof(char));
array[i] = word;
word = strtok(NULL," ");
i++;
}
*length = i;
return array;
}
It works correctly, but im still doubtful if it would ever crash. Specifically, why doesnt it throw a Segmentation error when i call: array[i] = (char *)malloc(sizeof(char)). ???
im still very new to Pointer this memory allocation concept; so would appreciate any helps.
Thanks