My problem is that it fails to assign memory to a (char*) pointer and I can't seem to understand why. I reviewed other similar questions, but I can't seem to grasp the answer. I'm trying to return a list of strings, but I know that in C I can't return char**, but I can do that via the char** parameter. My attempt is in the code snippets below.
in a.c
void list(char** fileList) {
int fileIndex = 0;
fileList = (char**)malloc(DIRECTORY_SIZE * sizeof(char*));
while ((dir = readdir(currentDirectory)) != NULL)
{
fileList[fileIndex] = (char*)malloc(FILENAME_LENGTH * sizeof(char)); //It fails here
fileList[fileIndex] = strncpy(fileList[fileIndex], dir->d_name, FILENAME_LENGTH);
fileIndex++;
printf("%s\n", fileList[fileIndex]);
}
}
in main.c
int main() {
char** fileList;
list(fileList);
for(int i = 0; i < 10; i++) {
printf("%s\n", fileList[i]);
}
}
I already looked over these questions: