Reputation: 9
I'm attempting to write a function that returns a 2 dimensional array. This code is supposed to return a list with the absolute paths of the input files.
char** findFiles(char rawFileList[100][1000], int numFiles) {
char (*absolutepaths)[1000] = malloc(1000 * sizeof(*absolutepaths));
int count1 = 0;
int files = numFiles; //unused
while (count1 < numFiles) {
if ((is_file(rawFileList[count1])) != 0) {
char *path = realpath(rawFileList[count1], NULL);
strcpy(absolutepaths[count1], path);
} else { //it is d
listFilesRecursively(absolutepaths[count1]);
}
count1++;
}
printf("%s", absolutepaths[1]);
return absolutepaths;
}
But I'm getting this error message:
error: returning ‘char (*)[1000]’ from a function with incompatible return type ‘char **’ [-Werror=incompatible-pointer-types]
68 | return absolutepaths;
I tried to resolve this issue but I have only drawn blanks so far. Any advice or help anyone can offer would be much appreciated.
Upvotes: 1
Views: 113
Reputation: 67526
It is quite common (not only for beginners) misunderstanding.
**
pointers are not 2D arrays or pointers to arrays.
You need to return a pointer to the array or (if the size of the array is not known or passed as a parameter a pointer to void
int (*functionReturnninPointerToArray(int array[][100]))[100]
{
/* .... */
return array;
}
void* functionReturnninPointerToVLA(size_t cols, int array[][cols])
{
/* .... */
return array;
}
The first one can be also written as
typedef int array100[100];
array100 *functionReturnninPointerToArray(array100 *array)
{
/* .... */
return array;
}
Upvotes: 2