I'm trying to create a function that returns an array of strings in C. I've looked at some other questions on the site and tried some of the solutions, but nothing seems to work. I've tried (1) simply changing the function to change the array contents rather than returning it, and I've tried (2) explicitly returning the arrayz, but neither seems to work. Approach #1: In one file, the function:
int getArray(int num, char** array)
{
int i;
for (i = 0; i < num; i++)
array[i] = "whatever";
return 0;
}
In another file, I call the function:
char** array = malloc(sizeof(char*) * num));
getArray(num, files);
When I try printing the contents of the array files, nothing shows up. Approach #2: In one file, the function:
char** getArray(int num)
{
int i;
char** array = malloc(sizeof(char*) * num));
for (i = 0; i < num; i++)
array[i] = "whatever";
return array;
}
In another file, I call the function:
char** files = getArray(num);
Again, when I try printing the content of files, nothing shows up.
I have checked and the header file where getFiles()
is being defined has been included in the file where I make the call. I'm not sure what's happening.