Monday, September 27, 2010

whooooaaa....

Holy smokes it worked.



I had to draw it out, and figure out how to NOT intentionally declare what I wanted to return (which is where void* came into play), but it worked. I don't 100% know why..... but it's good enough for now!

The WRONG way I was doing it was that I was treating the FILE data type like I would INT. A shorter version of the wrong way:

FILE loadfile(FILE *filepointer)
{
filepointer = fopen("test.plx", "rb");
return filepointer;
}

int main()
{
FILE *plxfilepointer;
plxfilepointer = loadfile(plxfilepointer);
...
}

That threw a big fit with compiler errors like "incompatible types when assigning to type 'struct FILE *' from type 'FILE'|"

So what I realized is that FILE is special somehow and I'd have to cheat a bit. Ultimately I wanted to return a pointer to the right place. So I knew I could declare the file pointer in main(), but then I needed to pass THAT variable a legit address to "be".

I had to get my function to want to return a pointer of any kind, not a FILE pointer.... I still don't think I understand this fully.

What I also don't understand fully is this:

plxfilepointer = loadfile(&plxfilepointer);

Ok, so I pass it the address of my FILE pointer, and then what?

void* loadfile(FILE **filepointer)
{
*filepointer = fopen("test.plx", "rb");
return *filepointer;
}

Am I doing (FILE **filepointer = &plxfilepointer)? That seems circular. The address of my FILE pointer is copied to the function as a pointer to a pointer?

return *filepointer is returning a pointer (because I'm dereferencing a pointer to a pointer, which is a pointer) and making it equal to plxfilepointer.

I understand why THAT works... I wanted to return a pointer to my pointer in main(). I just don't understand why passing the address of my FILE pointer to the function worked.

No comments:

Post a Comment