Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Accessing structure members indirectly

Reply
Thread Tools

Accessing structure members indirectly

 
 
Kavya
Guest
Posts: n/a
 
      10-27-2006
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?

 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      10-27-2006
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?


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 ___
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Bart
Guest
Posts: n/a
 
      10-27-2006
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.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-27-2006
"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?


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
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      10-27-2006
In article < .com>,
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?


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
 
Reply With Quote
 
Kavya
Guest
Posts: n/a
 
      10-27-2006

Thanks alot everyone.

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-27-2006
Kavya <> wrote:

> 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.
 
Reply With Quote
 
Kavya
Guest
Posts: n/a
 
      10-27-2006

Christopher Benson-Manica wrote:
> Kavya <> wrote:
>
> > 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?

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-27-2006
Kavya <> wrote:

> > 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.
 
Reply With Quote
 
Kavya
Guest
Posts: n/a
 
      10-27-2006

Christopher Benson-Manica wrote:
> Kavya <> wrote:
>
> > > 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a method indirectly swisscheese Python 4 03-05-2006 11:16 AM
A dllimport variable is accessed indirectly through a variable hsharsha@gmail.com C++ 2 02-11-2006 02:26 PM
Accessing WebService indirectly Stefan Rosi ASP .Net Web Services 1 12-13-2004 08:40 PM
Reading ENV vars in Solaris - Indirectly - help! Wells Java 5 05-17-2004 11:23 AM
referencing an object attribute sort of indirectly from within list python newbie Python 4 11-30-2003 12:34 PM



Advertisments