Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Address of struct in struct

Reply
Thread Tools

Address of struct in struct

 
 
Borealis
Guest
Posts: n/a
 
      06-07-2010
Hi,

this might be a silly question but I really got stuck here.

Suppose I have:

struct A {
int a;
int b;
int c;
};

struct B {
int x;
int y;
struct A z;
}

struct B params[2];

void doSomethingWithParams(struct B* arg)
{
/* Do something with parameters */
}

void main(void)
{
doSomethingWithParams(params[1].&z);
}

The function-call in main does not work. I get an error: expected a
field name

Is this a limitation of the c language? Is it not possible to get the
address of struct in struct? What would be a alternative to this? I
know I could have static variables for z and assign it as a pointer to
params (with struct A* z in B). But this does not hold relevant
information at one point then...

Thanks for your tips and hints!
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      06-07-2010
On Jun 7, 8:48*am, Borealis <listwo...@gmail.com> wrote:
> Hi,
>
> this might be a silly question but I really got stuck here.
>
> Suppose I have:
>
> struct A {
> * int a;
> * int b;
> * int c;
>
> };
>
> struct B {
> * int x;
> * int y;
> * struct A z;
>
> }
>
> struct B params[2];
>
> void doSomethingWithParams(struct B* arg)
> {
> * /* Do something with parameters */
>
> }
>
> void main(void)
> {
> * doSomethingWithParams(params[1].&z);
>


&params[1].z

Is what you're after.

Tom

 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      06-07-2010
Tom St Denis <> wrote:
> On Jun 7, 8:48Â*am, Borealis <listwo...@gmail.com> wrote:
> > this might be a silly question but I really got stuck here.
> >
> > Suppose I have:
> >
> > struct A {
> > Â* int a;
> > Â* int b;
> > Â* int c;
> > };
> >
> > struct B {
> > Â* int x;
> > Â* int y;
> > Â* struct A z;
> > }
> >
> > struct B params[2];
> >
> > void doSomethingWithParams(struct B* arg)
> > {
> > Â* /* Do something with parameters */
> >
> > }
> >
> > void main(void)
> > {
> > Â* doSomethingWithParams(params[1].&z);


> &params[1].z


> Is what you're after.


But note that this gives you the address of the struct A within
struct B (and not the address of a struct B), so the doSomething-
WithParams() should have a prototype of

void doSomethingWithParams(struct A* arg);

Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
 
Reply With Quote
 
Borealis
Guest
Posts: n/a
 
      06-07-2010
On 7 Jun., 14:59, j...@toerring.de (Jens Thoms Toerring) wrote:
> Tom St Denis <t...@iahu.ca> wrote:
>
>
>
> > On Jun 7, 8:48*am, Borealis <listwo...@gmail.com> wrote:
> > > this might be a silly question but I really got stuck here.

>
> > > Suppose I have:

>
> > > struct A {
> > > * int a;
> > > * int b;
> > > * int c;
> > > };

>
> > > struct B {
> > > * int x;
> > > * int y;
> > > * struct A z;
> > > }

>
> > > struct B params[2];

>
> > > void doSomethingWithParams(struct B* arg)
> > > {
> > > * /* Do something with parameters */

>
> > > }

>
> > > void main(void)
> > > {
> > > * doSomethingWithParams(params[1].&z);

> > &params[1].z
> > Is what you're after.

>
> But note that this gives you the address of the struct A within
> struct B (and not the address of a struct B), so the doSomething-
> WithParams() should have a prototype of
>
> void doSomethingWithParams(struct A* arg);
>
> * * * * * * * * * * * * * * * Regards, Jens
> --
> * \ * Jens Thoms Toerring *___ * * *j...@toerring.de
> * *\__________________________ * * *http://toerring.de



That's right, yes. Typo... Thanks for bringing me back on track!
 
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
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
address of a struct or a pointer to a struct? Johs32 C Programming 5 03-12-2006 01:15 AM
struct in struct Gunnar G C++ 14 06-02-2004 06:43 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57