C FILE pointer, why not just use the FILE type directly?

I know that in C we have to declare a pointer to type FILE. Why can't we use the FILE type directly instead of using a pointer to FILE?

FILE *file;
FILE file; // Why can't we use this instead?

      

+3


source to share


2 answers


A FILE

- resource. This file exists only once and cannot be moved. This makes FILE

C meaningless semantically. Whenever you have a resource, you refer to the resource by passing a pointer to the resource, which is what the C API does.



(Moreover, the definition FILE

is private to the implementation, not the part of the standard that is an essential part of the freedom you want to implement.)

+5


source


C originally did not have the ability to pass structures by value, this was added in ANSI C89.

FILE

would not work in K&R C. Since it FILE *

works just fine, there was no need or benefit to change the functions to work with FILE

instead of FILE *

when ANSI C was compiled.



As Kerrek SB points out, another benefit FILE *

is that there FILE

can be a struct typedef'd tag, and that leaves the implementation free to implement whichever is better, without breaking any working programs.

+1


source







All Articles