Help: Resizing an Array of Pointers
I'm hoping somebody can help me with an issue I'm running into while trying to resize an array of pointers. Part of the program I'm writing reads in a directory from Windows and stores each folder into an array so I know how many folders are in that directory and what those folders are.
As expected, I can't guarantee each folder has the same number of directories, so I want to have code in place that resizes the array to be larger if it hits the max initial size.
Right now I have the following for doing this:
*initial declaration of array*
int arraySize = 64;
char* textList[arraySize]; // the array in question
memset(textList,'\0',arraySize); // setting all values of array to null since it won't initially be full anyway. That way I can run normal print-outs and it will end where it should depending on it's size.
Right now the code crashes on item #85 after the increase. I've determined that something is up with my reassignment I think, because if I assign say textList2 to something and print out textList2 it displays fine. However after reassigning textList to textList2 and trying to print textList it crashes.
arraySize = arraySize * 2; // double array size
char * textList2[arraySize]; // create temporary array
memset(textList2,'\0',arraySize); // set all values to null like before
textList2[j] = textList[j]; // place all items from textList into textList2
free(textList); // free memory at textList
*textList = *textList2; // reassign textList to textList2
Can anyone help point out what I'm doing wrong? Any help would be greatly appreciated.