FreeRTOS-FAT: Passing around file handle pointers

I need to know if I am overlooking something, or just doing something wrong. I have a function to do a thing, in that function I declare FF_FILE *fileIn and then call another function to open that file and read some header info. fileIn is passed as a function parameter FF_FILE *fileHandle. The debugs in my opening function show the file is being opened and read, and my data is coming in correct, but when it returns and I check the file handle it always fails (!fileIn) and I abort. If I open the file before calling that function (and comment out the ff_fopen in that function) everything works. So why isn’t the handle coming back to me in the original code?

FF_FILE *fileIn;
open_dat_file(&hdrStruct, fileIn);
if (!fileIn)
{
    printf("FILE NOT OPENED\n);
    return 1;
}
....do stuff .....
.
.
.
void open_dat_file(DAT_HDR_T *header, FF_FILE *fileHandle)
{
    ff_fopen(..bla bla bla)
    if (!fileHandle)
    {
        return;
    }
.
.
.
.

I have already made the modifications to get around it, but I just want to make sure I don’t end up with similar issues in the future.

Michael

It looks like you are ignoring the pointer to the file returned by ff_fopen.

Sorry, that was an error typing up the post. It is actually fileHandle = ff_fopen(bla bla bla).

Michael

you pass a pointer into a function, it will not change until you manully return as value.
for example:

int a;
void function( int *p)
{
p=&a;
}
///////////////////////////////

int *value=NULL;
int main(void)
{
fuction(value);
//value is NULL here
}

@Yangnaan has correctly identified the issue with your code. You need to return the file handle from the function:

FF_FILE *fileIn = open_dat_file(&hdrStruct);
if (!fileIn)
{
    printf("FILE NOT OPENED\n");
    return 1;
}
....do stuff .....
.
.
.
FF_FILE* open_dat_file(DAT_HDR_T *header)
{
    FF_FILE * handle = ff_fopen(..bla bla bla)
    if (!fileHandle)
    {
        return NULL;
    }
    return handle;
}

If you do not want to use the return value and instead use the parameter, you need to use double pointer.

Ugh, yeah stupid mistake, thanks guys. That was at the end of a long day and my thinking wasn’t there.

Michael