The program aims to get the first word and an array of remaining words. For example, if line = "a bb cc dd ee", then key should be a, keySet should be a pointer to an array of {bb,cc,dd,ee}.
I try to dynamically allocate memory to char** keySet, but keySet output always ee ee ee ee. It seems that keySet is not the pointer to the first element of array but point to the last element. What's the problem in my function?
void allocateKeySet(char* line){
int count = 0;
char* token = strtok(line, " ");
char key[17];
char tempKey[17];
char** keySet;
sscanf(strtok(NULL, " "), " %s", key);
keySet = calloc(1, sizeof(char*));
while((token = strtok(NULL, " ")) != NULL){
sscanf(token, " %s", tempKey);
keySet[count++] = tempKey;
keySet = realloc(keySet, (count+2) * sizeof(char*));
}
printf("key: %s\n", key);
printf("keySet: ");
for(int i = 0; i < count - 1; i++){
printf("%s ", keySet[i]);
}
}
e.g. line:
"a bb cc dd ee"
expected output:
key: a
keySet: bb cc dd ee
My output:
key: a
keySet: ee ee ee ee