What exactly is the FILE keyword in C?

后端 未结 4 882
无人共我
无人共我 2020-11-30 02:54

I\'ve started learning some C as a hobby and have blindly used FILE as a declaration for file pointers for quite some time, and I\'ve been wondering. Is this a keyword or sp

4条回答
  •  粉色の甜心
    2020-11-30 02:55

    FILE is an identifier used as a typedef name, usually for a struct. The stdio library usually has something like

    typedef struct {
       ...
    } FILE;
    

    somewhere. All stdio functions dealing with FILE pointers know the contens of ... and can access the structure members. The C programmers must use functions like fopen, feof, ferror, ungetc etc to create and operate on FILE structures. Such types are called opaque (i.e. you can´t peek inside them but must use accessor functions).

    Why is it defined as a pointer?

    It isn't. It's a struct to which your code declares a pointer. Note the asterisk in your

    FILE* fp;
    

    which is another example of why the asterisk should go with the variable identifier, not the type name:

    FILE *fp;
    

提交回复
热议问题