Does *&s1 refer to the first member of structure variable s1

  • Thread starter lovecreatesbea...
  • Start date
L

lovecreatesbea...

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;
}

/*
$ gcc -ansi -pedantic -W -Wall a.c
$ ./a.exe

3

$
*/
 
J

Jens Thoms Toerring

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
 
E

Eric Sosman

(e-mail address removed) 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.
 
C

Christopher Benson-Manica

Eric Sosman said:
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.

Can you cite C&V on this? I know it's there somewhere, but my first
guess (6.7.2.1) proved to be wrong.
 
R

Ralf Damaschke

Christopher said:
Can you cite C&V on this? I know it's there somewhere, but my
first guess (6.7.2.1) proved to be wrong.

It's inside 6.7.2.1 p13:

[...] A pointer to a structure object, suitably converted, points
to its initial member [...], and vice versa.

Ralf
 
C

Christopher Benson-Manica

Ralf Damaschke said:
It's inside 6.7.2.1 p13:
[...] A pointer to a structure object, suitably converted, points
to its initial member [...], and vice versa.

Darn, so it is, albeit buried in a paragraph that seems initially to
be about something else. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top