473,804 Members | 3,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2073
lovecreatesbea. ..@gmail.com <lo************ ***@gmail.comwr ote:
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*********@su n.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)gma il.com | don't, I need to know. Flames welcome.
Jul 5 '07 #4
Christopher Benson-Manica wrote:
Eric Sosman <Er*********@su n.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.dew rote:
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)gma il.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
1108
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) which invokes a webpage when a button is pressed. I want to change the size of that webpage, but when I do things like Explorer.Width=400 It modifies the instance of explorer the button is invoked from....rather than the one that I launch. How...
19
6879
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
1389
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 member variable. At Page_Load time I initialize this member variable I have a host form which contains the user control I have a button and a label on the host form (in addition to the user control) Now when I click the host-form button, it is...
10
3831
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 For...Next and If...End If blocks can have their own variables. So,
5
60137
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 front of and behind the string. How do I make this work?
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10320
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7609
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.