Why would you cast a struct as a char* when using fread in c?
Lets say I have a "Passenger" struct, which has a field for a name.
开发者_如何学CIf I do (like the syntax of my book shows):
fread(&passenger, sizeof(Passenger), 1, in_fp);
printf("%s", (*passenger).first_name)
I get a segmentation fault, but if I do:
fread( (char *)passenger, sizeof(Passenger), 1, in_fp);
printf("%s", (*passenger).first_name)
the name read from the file will be printed out.
Looks to me as if 'passenger' is a pointer. If you take &passenger, you are passing the address of the pointer to fread. When you cast it, you are telling fread to treat it as a pointer to a char buffer instead of a pointer to a Passenger.
You probably have a pointer to a Passenger, not a Passenger:
fread(passenger, sizeof(Passenger), 1, in_fp); printf("%s", (*passenger).first_name)
Will most likely do what you want.
In the early days of C language, C had no void *
type (it appeared later, was borrowed from C++ actually) and type char *
was used as the generic "raw memory" pointer type instead. So, even to this day you might see this habitual rudimentary use of type char *
as the generic pointer type in the code, and see other pointer types explicitly converted to and from char *
in generic pointer context. I'd guess that the code you quoted does this for that specific reason. (Unless it was you who put this char *
cast there. In that case I can only ask "Why?".)
In modern C the first parameter of fread
has void *
type, meaning that no cast is necessary. This
fread(passenger, sizeof *passenger, 1, in_fp);
whill work just as well.
Your &passenger
version makes no sense, since apparently the original intent was to read data into the location passenger
points to, not into the passenger
pointer object itself.
精彩评论