Are File* and Char* the same or is there a difference?

  • A+
Category:Languages

When I learned about pointers learned that char* was the operator for pointers but when I look at tutorials online I see people use file*. So I'm wondering if there a reason to use one over the other.

 


In C FILE* and char* are two completely different things.

FILE* is a pointer to a FILE structure which represents a file opened with something like fopen. There's a whole family of f functions that interact with these, like fread and fclose.

char* is a pointer to characters, which is to say in C terms, a "string" or a "buffer". The str family of functions deal with these, like strncpy and strcmp and such.

These are in no way similar except that they're both pointers, but given that you're talking about C, pointers are basically the C philosophy and are omnipresent.

Now C is considered a strongly typed language which means that any given function has extremely specific expectations for arguments and return type. If you try and supply a FILE* pointer to a function expecting char* you'll get a compile error. They're not compatible, and not interchangeable.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: