LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   why FILE* fp? why not FILE fp? (https://www.linuxquestions.org/questions/programming-9/why-file%2A-fp-why-not-file-fp-202021/)

skywalker27182 07-06-2004 11:37 PM

why FILE* fp? why not FILE fp?
 
hi everybody,
my question is: why do we always use FILE * and not just FILE while declaring a file pointer

Kumar 07-07-2004 01:03 AM

We always use FILE * because the function fopen returns a file pointer . So to collect the returned value, which is an address, we use a file pointer.

skywalker27182 07-07-2004 02:48 AM

so, will the following work?

Code:

main()
{
    ...

    FILE fp;
    &fp = fopen(...);

    ...
}


itsme86 07-07-2004 10:25 AM

No, C doesn't allow that kind of lvalue. I suggest you read up on pointers. What they are and where/why they're useful.

jinksys 07-07-2004 04:36 PM

In other words,
we declare a FILE *fp because when we call fopen a FILE is created in memory, so we use the *fp to reference it from our code.

As you can see from fopen's declaration:

FILE *fopen(const char *path, const char *mode);

" FILE *fopen " means it returns a pointer to type FILE, so as I said above,
we declare a FILE *fp so we can use that pointer.

skywalker27182 07-07-2004 11:03 PM

okkk, understood. thanks all for ur replies. will read up on pointers.


All times are GMT -5. The time now is 05:40 PM.