473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

$
*/

Jul 5 '07 #1
5 2054
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
Jul 5 '07 #2
lovecreatesbea...@gmail.com 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.

--
Er*********@sun.com
Jul 5 '07 #3
Eric Sosman <Er*********@sun.comwrote:
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.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jul 5 '07 #4
Christopher Benson-Manica wrote:
Eric Sosman <Er*********@sun.comwrote:
> 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.
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

Jul 5 '07 #5
Ralf Damaschke <rw****@gmx.dewrote:
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.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jul 5 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: haxmya | last post by:
#1 what is that variable length scrolling stuff on this forum, it seems to be a bug that messes up my searches. #2 More Importantly. I have developed an Internet Explorer Toolbar (Band Object)...
19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
2
by: JohnK | last post by:
I have a user control with 2 buttons on it & 1 label.... as each button is pressed, they set a member variable within the class and sets the label test. I also have a get/set property for the...
10
by: ElanKathir .S.N | last post by:
Hi all ! VB.NET adds the ability to create variables that are visible only within a block. A block is any section of code that ends with one of the words End , Loop , or Next . This means that...
5
by: Zammy | last post by:
I'm guessing this has been explained before, but I can not seem to phrase the search correctly. I am trying to use a string (strString) in a VBA select query. I want to include the * wildcard in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.