473,466 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accessing structure members indirectly

Here is the code

int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}

Will the last printf always result in correct output? Is it safer to
access data members of structure this way?

Oct 27 '06 #1
14 3683
Kavya <Le******@gmail.comwrote:
Here is the code
int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}
Will the last printf always result in correct output? Is it safer to
access data members of structure this way?
You are guaranteed that address of the first element of a structure
is always at the address of the structure, so you can safely access
the member 'a' of your structure that way (if it's a good idea is
a totally different question). For the other members it's not safe,
e.g.

printf( "%d", * (int *) ((char *) ptr + sizeof(int)));

is _not_ guranteed to print out the value you stored in the member
'b' since the compiler is allowed to insert as many padding bytes
between the structure members as it likes.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Oct 27 '06 #2
Kavya wrote:
Here is the code

int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}

Will the last printf always result in correct output? Is it safer to
access data members of structure this way?
Why not just use an array instead of inventing convoluted ways to shoot
yourself in the foot?

Regards,
Bart.

Oct 27 '06 #3
"Kavya" <Le******@gmail.comwrote:
Here is the code

int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}

Will the last printf always result in correct output?
Yes, for the first member. The first member of a struct must always have
the same address as the struct itself. This is required by the Standard.
For any other member, you don't know that. There could be padding
between a and b, so ((int *)&s)+1 might not point at s.b.
Is it safer to access data members of structure this way?
Not at all. Accessing them normally is the best way.

Richard
Oct 27 '06 #4
In article <11**********************@b28g2000cwb.googlegroups .com>,
Kavya <Le******@gmail.comwrote:
>Here is the code
>int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}
>Will the last printf always result in correct output?
You haven't defined what output it is that you feel to be "correct".
>Is it safer to
access data members of structure this way?
Safer than what??

What the C standards say is that:

1) the first named member of the structure will always be at offset 0
from the beginning of the structure

2) each additional member will be placed in increasing address order

3) there may be internal padding at any point after the first member,
including at the end of the structure

4) bitfields generally follow the increasing address rule, but the order
of the bits within the internal storage allocation size that
the implementation uses for bitfields, is up to the implementation
5) special meaning for a bitfield width of 0
The first rule ensures that in your example *(int *)&s corresponds
to s.a. However, *(1 + (int *)&s) will *not* necessarily be s.b
because of rule 3.

--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Oct 27 '06 #5

Thanks alot everyone.

Oct 27 '06 #6
Kavya <Le******@gmail.comwrote:
int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}
Will the last printf always result in correct output?
No, but only because it is implementation-defined whether a newline
is required after the last line of output:

printf("%d\n",*(int*)ptr);

P.S. Main returns int, but you didn't return one. Do so and make your
execution environment happy.

--
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.
Oct 27 '06 #7

Christopher Benson-Manica wrote:
Kavya <Le******@gmail.comwrote:
int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}
Will the last printf always result in correct output?

No, but only because it is implementation-defined whether a newline
is required after the last line of output:
Sorry. I didn't get that.
P.S. Main returns int, but you didn't return one. Do so and make your
execution environment happy.
Isn't that implicit?

Oct 27 '06 #8
Kavya <Le******@gmail.comwrote:
No, but only because it is implementation-defined whether a newline
is required after the last line of output:
Sorry. I didn't get that.
The English version of that sentence is "Things might not work like
you expect if you don't end your output with a newline."
P.S. Main returns int, but you didn't return one. Do so and make your
execution environment happy.
Isn't that implicit?
Only in C99; you are probably not using a C99 compiler. (In English:
There are two different standards that govern the C language - C89 and
C99; many or most C compilers conform to C89 rather than C99. In C89,
if you fail to return a value from main, your shell or whatever you're
executing your program in gets a random return value.)

--
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.
Oct 27 '06 #9

Christopher Benson-Manica wrote:
Kavya <Le******@gmail.comwrote:
No, but only because it is implementation-defined whether a newline
is required after the last line of output:
Sorry. I didn't get that.

The English version of that sentence is "Things might not work like
you expect if you don't end your output with a newline."
P.S. Main returns int, but you didn't return one. Do so and make your
execution environment happy.
Isn't that implicit?

Only in C99; you are probably not using a C99 compiler. (In English:
There are two different standards that govern the C language - C89 and
C99; many or most C compilers conform to C89 rather than C99. In C89,
if you fail to return a value from main, your shell or whatever you're
executing your program in gets a random return value.)
Thanks for the these pointers.

Oct 27 '06 #10
Kavya wrote:
Here is the code

int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}

Will the last printf always result in correct output? Is it safer to
access data members of structure this way?
is offsetof() any use to you? I'm not sure what you are trying to do.
--
Nick Keighley

And so when you think of the future,
imagine a Pentium stamping on a man's face,
forever.

Oct 27 '06 #11
On Fri, 2006-10-27 at 02:56 -0700, Kavya wrote:
Here is the code

int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}

Will the last printf always result in correct output? Is it safer to
access data members of structure this way?
I'm pretty sure that "(int) *ptr" will be equally valid and predictable.
Unfortunately, neither /is/ predictable, so it depends on what you mean
by "correct" output.

I believe there is no undefined behaviour, which may be a good
definition of "correct". (Of course, I could be wrong.)

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 27 '06 #12
On Fri, 2006-10-27 at 15:18 +0000, Andrew Poelstra wrote:
On Fri, 2006-10-27 at 02:56 -0700, Kavya wrote:
Here is the code

int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}

Will the last printf always result in correct output? Is it safer to
access data members of structure this way?
<snip my own incorrectness>
>
I missed your struct definition above. Yes, this is in fact correct and
predictable, but only for the first element. (As has been noted by
others.)

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 27 '06 #13
Andrew Poelstra <ap*******@false.sitewrites:
On Fri, 2006-10-27 at 02:56 -0700, Kavya wrote:
>Here is the code

int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}

Will the last printf always result in correct output? Is it safer to
access data members of structure this way?

I'm pretty sure that "(int) *ptr" will be equally valid and predictable.
Unfortunately, neither /is/ predictable, so it depends on what you mean
by "correct" output.
*ptr is of type struct node; you can't convert a struct value to int.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 27 '06 #14
Ark
Jens Thoms Toerring wrote:
Kavya <Le******@gmail.comwrote:
>Here is the code
>int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
printf("%d",*(int*)ptr);
}
>Will the last printf always result in correct output? Is it safer to
access data members of structure this way?

You are guaranteed that address of the first element of a structure
is always at the address of the structure, so you can safely access
the member 'a' of your structure that way (if it's a good idea is
a totally different question). For the other members it's not safe,
e.g.

printf( "%d", * (int *) ((char *) ptr + sizeof(int)));

is _not_ guranteed to print out the value you stored in the member
'b' since the compiler is allowed to insert as many padding bytes
between the structure members as it likes.
However, b can be portably printed using
printf("%d", *(int*)((char*)ptr + offsetof(struct node, b)));

As to the wisdom of this technique... Occasionally, one may need/want to
parse data consisting of "derived types" like
struct type1 {
int RTTI;
struct base_t base;
struct type1_specific more;
};
struct type3 {
int RTTI;
struct base_t base;
struct type3_specific more;
};
Here, an offsetof-based technique may be useful.

- Ark
Oct 28 '06 #15

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

Similar topics

2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
6
by: archilleswaterland | last post by:
structures typedef struct{ char name; int age; float balance; }account; account xyx; accout *ptr;
6
by: TB | last post by:
I understand the basics of finalization and implementing IDisposable and how one is guaranteed access to managed objects only when working through the IDisposable interface. My question is to what...
3
by: nkrisraj | last post by:
Hi, I have a following structure: typedef struct { RateData rdr; int RateID; char RateBalance; } RateInfo;
8
by: nkrisraj | last post by:
Hi, I have a following structure: typedef struct { RateData rdr; int RateID; char RateBalance; } RateInfo;
4
by: John | last post by:
I am having a problem accessing the members in a structure via an interface and Class. ***Development Code*** Structure Person Public Name as String End Structure Interface IData Public...
7
by: =?Utf-8?B?U3RlcGhhbmllIERvaGVydHk=?= | last post by:
I am trying to call some API functions written in C++ from my VB .NET 2003 application and am running into some difficulty in passing structures to a function. It is probably due to the types I'm...
4
by: Bas Wassink | last post by:
Hello all, I've been wondering about struct member names and reserved identifiers for some time now and I can't figure out whether the reserved identifier restrictions apply to struct members. ...
6
by: Sid | last post by:
All, See the below piece of C code snippet typedef struct _a { int ia; int ja; }A;
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.