Re: Does *&s1 refer to the first member of structure variable s1
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Thu, 05 Jul 2007 10:49:27 -0400
lovecreatesbea...@xxxxxxxxx wrote On 07/05/07 10:24,:
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. A pointer to a struct can be converted to a
pointer to the struct's first element (if it is possible
to make such a pointer at all), and vice versa.
This guarantee applies only to the *first* element,
not to any subsequent elements. In your sample, it is
*not* guaranteed that (int*)&s1+1 == &s1.m2.
--
Eric.Sosman@xxxxxxx
.
- Follow-Ups:
- Re: Does *&s1 refer to the first member of structure variable s1
- From: Christopher Benson-Manica
- Re: Does *&s1 refer to the first member of structure variable s1
- References:
- Does *&s1 refer to the first member of structure variable s1
- From: lovecreatesbea...@xxxxxxxxx
- Does *&s1 refer to the first member of structure variable s1
- Prev by Date: Re: Does *&s1 refer to the first member of structure variable s1
- Next by Date: Re: Does *&s1 refer to the first member of structure variable s1
- Previous by thread: Re: Does *&s1 refer to the first member of structure variable s1
- Next by thread: Re: Does *&s1 refer to the first member of structure variable s1
- Index(es):
Relevant Pages
|