Address of struct in struct

Discussion in 'C Programming' started by Borealis, Jun 7, 2010.

  1. Borealis

    Borealis Guest

    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!
     
    Borealis, Jun 7, 2010
    #1
    1. Advertising

  2. Borealis

    Tom St Denis Guest

    On Jun 7, 8:48 am, Borealis <> 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
     
    Tom St Denis, Jun 7, 2010
    #2
    1. Advertising

  3. Tom St Denis <> wrote:
    > On Jun 7, 8:48 am, Borealis <> 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
     
    Jens Thoms Toerring, Jun 7, 2010
    #3
  4. Borealis

    Borealis Guest

    On 7 Jun., 14:59, (Jens Thoms Toerring) wrote:
    > Tom St Denis <> wrote:
    >
    >
    >
    > > On Jun 7, 8:48 am, Borealis <> 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



    That's right, yes. Typo... Thanks for bringing me back on track!
     
    Borealis, Jun 7, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    1
    Views:
    7,341
    avnrao
    May 4, 2004
  2. Patrice
    Replies:
    0
    Views:
    925
    Patrice
    May 4, 2004
  3. Raterus
    Replies:
    0
    Views:
    465
    Raterus
    Jun 2, 2004
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,442
    Chris Fogelklou
    Apr 20, 2004
  5. Johs32
    Replies:
    5
    Views:
    378
    Barry Schwarz
    Mar 12, 2006
Loading...

Share This Page