Thread: Structs

  1. #1
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87

    Structs

    I'm having some issues with structs.

    If I have a couple of nested structs, is it possible to obtain the address of a member struct, or is this something that is not good practise. I.e. is it possible to obtain the address of fooStruct->barStruct->num ?. I am guessing that this is not possible, as the member would be something like an offset from the address of the encompassing struct (maybe??).
    Code:
    #include <iostream>
    
    using namespace std;
    
    typedef struct bar
    {
            int num;
    }BAR;
    
    typedef struct foo
    {
            BAR *barStruct;
    }FOO;
    
    int main(void)
    {
            FOO *fooStruct = new FOO;
            fooStruct->barStruct = new BAR;
    
            FOO *blahStruct = new FOO;
            blahStruct->barStruct = new BAR;
    
            fooStruct->barStruct->num = 10;
            blahStruct->barStruct->num = 11;
    
            cout << "fooStruct->barStruct->num: " << fooStruct->barStruct->num << endl;
            cout << "blahStruct->barStruct->num: " << blahStruct->barStruct->num << endl;
    
            return 0;
    }
    Also, if I do not allocate memory for barStruct member in both of the "fooStruct" and "blahStruct" then the "num" member seems to point at exactly the same location. I.e. updating one with a value changes the other. Why is this?
    Visual C++ .net
    Windows XP profesional

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >> is it possible to obtain the address of fooStruct->barStruct->num ?

    Yes. &( fooStruct->barStruct->num )


    >>Also, if I do not allocate memory for barStruct member in both of the "fooStruct" and "blahStruct" then the "num" member seems to point at exactly the same location. I.e. updating one with a value changes the other. Why is this?
    It is undefined behaviour, so anything can happen.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: Structs

    Originally posted by filler_bunny
    I'm having some issues with structs.

    If I have a couple of nested structs, is it possible to obtain the address of a member struct, or is this something that is not good practise. I.e. is it possible to obtain the address of fooStruct->barStruct->num ?.
    Sure; you can take the address of anything that lives in memory.

    I am guessing that this is not possible, as the member would be

    Also, if I do not allocate memory for barStruct member in both of the "fooStruct" and "blahStruct" then the "num" member seems to point at exactly the same location. I.e. updating one with a value changes the other. Why is this?
    At a guess? Coincidence. Dereferencing a pointer to unallocated memory is undefined; for the results you see, it's likely that, under the test conditions you had, the initial value for fooStruct->barStruct and blahStruct->barStruct was the same -- that is, they point at the same (uninitialized) place in memory.

  4. #4
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Yes. &( fooStruct->barStruct->num )
    That simple huh.

    Thanks for the responses, that is very helpful!
    Visual C++ .net
    Windows XP profesional

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM