We have a structure in C say
struct info{
int no;
char first_name[20];
char last_name[20];
char status;
}
At runtime when we try to access these members by their name, say info_var.no or info_var.first_name, or we use a pointer to the structure, info_ptr->no or info_ptr->first_name, how are these individual members accessed?
I mean, the structure will be stored as member by member along with some necessary padding, but how does the runtime or maybe the compiler, if replacement happens at compile time, access those individual members by their name?
I know a lot of it is implementation dependent but if anybody could throw some light on any implementation or just give an overview it would be really nice.
Accessing by a pointer or an object makes no difference, actually
info_ptr->nois equivalent to(*info_ptr).no.Actual member access is compiler-specific.
Say you have:
The following is the access code:
So the compiler, in this case, generates the binary knowing where
xis stored in memory relative toa– that is 8 bytes into theAinstance. This is because of padding.EDIT: I just saw that the question is about C. Regardless, should be the same :).