Thank you for your answer.
Why did I ask that... I have the following function which returns an array with the files from a folder. I modified that so now I can pass a pointer which will hold the length of the array. The function looks like this:
Code:
typedef struct
{
char *fileName;
int isFile;
} FileData;
FileData **fs_listFiles(const char *path, int *length)
{
DIR *dir;
struct dirent *ent;
FileData **fd;
int i;
dir = opendir (path);
if (dir != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
fd = (FileData **)realloc((void *)fd, sizeof(FileData*)*(i+1));
fd[i] = (FileData *)malloc(sizeof(FileData));
fd[i]->fileName = strdup(ent->d_name);
printf("%s\n",fd[i]->fileName); //here everything is ok
i++;
(*length)++;
}
closedir (dir);
return fd;
}
return NULL;
}
int main()
{
int length = 0;
FileData **res = fs_listFiles("/home/user",&length);
printf("%d\n",length); //length is let`s say 53, ok...
return 0;
}
The problem is that doesn`t matter what value the length variable holds, if I print this to the screen: printf("%s\n",res[0]->fileName); it gives me segmentation fault. According to the length (which is let`s say 53, it should be an index 0 in the array);
I`m sure is something wrong with the function itself, but i can`t figure out what, because if I printf the file name or the current index or anything else in the function, it is ok, but if I try to use the array returned from the function doesn`t matter what I do, it gives me segmentation fault.
Thank you.