I'm trying to make a dynamic char array, but I'm running into issues. I get segmentation fault when I try to add anything to the array.
static int arrays()
{
int INITIAL = 1;
char **all_words;
all_words = (char**)malloc(INITIAL*sizeof(*all_words));
int currentSize = INITIAL;
for(int i = 0; i < counter; i++){
all_words = realloc(all_words,currentSize*sizeof(*all_words));
strcpy(all_words[i], "hello");
currentSize++;
}
for(int i = 0; i < counter; i++){
printf("%s ", all_words[i]);
}
}
I pretty much copied this from a guide online, so I'm not sure why it wouldn't work.
mallocandreallocsucceeded.strcpywithstrdupso you actually allocate memory for the strings.