Pointers to nested structs

Discussion in 'C++' started by Joolz, Oct 3, 2005.

  1. Joolz

    Joolz Guest

    Could someone please give me an example of how to make pointers to
    nested structures? Pointers to normal structures are no problem...

    struct structA {
    int a;
    }structA_s;

    structA *pStructA = &structA_s;

    But when I've got a nested structure, I can't make a pointer to that
    structure.

    struct structA {
    int a;

    struct structB {
    int b;
    }structB_s;

    }structA_s;
    structA *pStructA = &structA_s;

    Now how do I make a pointer to an object of structB? I -can- reference
    members of an object of a nested struct with i.e
    pStructA->structB_s.b but that's without pointers.

    Thanks in advance.
    Jules
     
    Joolz, Oct 3, 2005
    #1
    1. Advertising

  2. Joolz

    Zara Guest

    On 3 Oct 2005 05:14:29 -0700, "Joolz" <> wrote:

    >Could someone please give me an example of how to make pointers to
    >nested structures? Pointers to normal structures are no problem...
    >
    >struct structA {
    >int a;
    >}structA_s;
    >
    >structA *pStructA = &structA_s;
    >
    >But when I've got a nested structure, I can't make a pointer to that
    >structure.
    >
    >struct structA {
    >int a;
    >
    > struct structB {
    > int b;
    > }structB_s;
    >
    >}structA_s;
    >structA *pStructA = &structA_s;
    >
    >Now how do I make a pointer to an object of structB? I -can- reference
    >members of an object of a nested struct with i.e
    >pStructA->structB_s.b but that's without pointers.
    >
    >Thanks in advance.
    >Jules


    structA::structB *pStructB=&structA_s.structB_s;

    that's all
     
    Zara, Oct 3, 2005
    #2
    1. Advertising

  3. Joolz

    Joolz Guest

    Cheers, now I can access the members by using pStructB->b , but now
    (for me) the nesting-aspect is gone.

    I want to access b like pStructA->pStructB->b

    Thanks though :)
     
    Joolz, Oct 3, 2005
    #3
  4. Joolz

    mlimber Guest

    Joolz wrote:
    > Cheers, now I can access the members by using pStructB->b , but now
    > (for me) the nesting-aspect is gone.
    >
    > I want to access b like pStructA->pStructB->b
    >
    > Thanks though :)


    You can simply access your structB elements with the . operator:

    pStructA->structB_s.b

    You could make a pointer as a member of structA if your really want to,
    but then you'll need to initialize it in the constructor:

    struct A
    {
    int aDatum;
    struct B
    {
    int bDatum;
    };
    B* const pB;

    A() : pb( new B ) {}
    ~A() { delete pb; }
    };

    A *pA = new A;
    pA->pB->bDatum = 42;
    // ...
    delete pA;

    Or better you could use std::auto_ptr or boost::scoped_ptr for pB.

    Cheers! --M
     
    mlimber, Oct 3, 2005
    #4
  5. Joolz

    Joolz Guest

    Thanks, that works - allthough it looks a tad difficult...

    Just to clear some things up, it isnt possible to access the variables
    like you've shown (pA->pB->variable) but without the new/delete and
    constructor/destructor things? That would help me out alot.

    Cheers
    J
     
    Joolz, Oct 3, 2005
    #5
  6. Joolz

    mlimber Guest

    Joolz wrote:
    > Thanks, that works - allthough it looks a tad difficult...
    >
    > Just to clear some things up, it isnt possible to access the variables
    > like you've shown (pA->pB->variable) but without the new/delete and
    > constructor/destructor things? That would help me out alot.
    >
    > Cheers
    > J


    You can, but the member pB needs to be a pointer. You should generally
    avoid new and delete if they aren't necessary, which translates to
    allocating objects automatically on the stack.

    Perhaps you just think that pA->pB->variable looks prettier; you could
    do the same sort of thing with the . operator and references (which are
    like const pointers) in many cases:

    struct A
    {
    int aDatum;
    struct B
    {
    int bDatum;
    };
    B b;
    };

    void Foo( A& a )
    {
    a.aDatum = 0xc0ffee;
    a.b.bDatum = 42;
    }

    void Bar()
    {
    A a;
    Foo( a );
    // ...
    }

    Cheers! --M
     
    mlimber, Oct 3, 2005
    #6
  7. Joolz

    Joolz Guest

    lol - 0xc0ffee

    Well you're right though, the -> operator is easier to 'spot' for me
    than a small dot. It's a weird reason to use pointers though, but then
    again , it kinda works the same way except its a little more work.

    Cheers
     
    Joolz, Oct 3, 2005
    #7
  8. Joolz

    mlimber Guest

    Joolz wrote:
    > lol - 0xc0ffee


    The best was when I worked for a guy named Bob Defeo because I could
    spell his name in hex (0xB0BDEFE0).

    > Well you're right though, the -> operator is easier to 'spot' for me
    > than a small dot. It's a weird reason to use pointers though, but then
    > again , it kinda works the same way except its a little more work.


    It does kinda work the same way, but the dot operator has one major
    advantage: there's no chance of dereferencing a null pointer because
    you can't forget to initialize or reseat a reference. (Of course, if
    the pointer is const, this criticism is mitigated.) Also, the pointer
    takes up an extra chunk of otherwise unneeded space, which may be an
    issue if you have many instances of the class or limited memory
    requirements. Finally, those who read your code later (including you)
    will have one more thing to figure out. For those reasons, I'd
    discourage the practice of using -> over . just for aesthetic reasons.

    Cheers! --M

    PS, You could always do something really nasty and overload the ->
    operator in the nested struct. That could give you the same effect, but
    again, it's probably better to go with the . operator. You could always
    use spaces to help set them off, e.g., a . b . c = 5;
     
    mlimber, Oct 4, 2005
    #8
  9. Joolz

    Marcus Kwok Guest

    mlimber <> wrote:
    > Joolz wrote:
    >> lol - 0xc0ffee

    >
    > The best was when I worked for a guy named Bob Defeo because I could
    > spell his name in hex (0xB0BDEFE0).


    I saw in a homework assignment where they would initialize memory with
    0xDEADBEEF.

    --
    Marcus Kwok
     
    Marcus Kwok, Oct 6, 2005
    #9
  10. Joolz

    red floyd Guest

    Marcus Kwok wrote:
    > mlimber <> wrote:
    >
    >>Joolz wrote:
    >>
    >>>lol - 0xc0ffee

    >>
    >>The best was when I worked for a guy named Bob Defeo because I could
    >>spell his name in hex (0xB0BDEFE0).

    >
    >
    > I saw in a homework assignment where they would initialize memory with
    > 0xDEADBEEF.
    >


    My favorite was 0xDEFACEABL, I've also seen 0xDEADBABE

    In addition, 1337-sp33k makes it possible to have even more descriptive
    identifiers (I use them for "magic numbers"/signatures in data headers).
     
    red floyd, Oct 6, 2005
    #10
    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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    661
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    578
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    655
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,441
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    507
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page