lovecreatesbea...@gmail.com <lo***************@gmail.comwrote:
Does the expression *(int *)&s1 below inside the printf() statement
guarantee to refer to the first member of the structure variable s1?
I've tried the code and it seems that it works that way. The C
standard states this? Thank you very much.
#include <stdio.h>
struct S{
int m1, m2, m3;
};
int main(void)
{
struct S s1 = {3, 4, 5};
printf("%d\n", *(int *)&s1);
return 0;
}
Yes, this is ok since it's guaranteed that the first member of a
structure always is at the very start of the structure without
any padding bytes before the first member.
$ gcc -ansi -pedantic -W -Wall a.c
$ ./a.exe
The compiler warning flags don't help you much if you use casts -
they tell the compiler that you know what you're doing, so don't
expect too many helpful warnings...
Regards, Jens
--
\ Jens Thoms Toerring ___
jt@toerring.de
\__________________________
http://toerring.de