structs with fields that are structs

Discussion in 'C Programming' started by Patricia Van Hise, Apr 5, 2004.

  1. Is it possible to access a field of a struct which is a field of
    another struct? Ex. struct subStr{
    int num1;
    int num2;
    };
    struct myStr {
    int num3;
    subStr *lock;
    };

    struct myStr *info;

    I tried to store a value in num1 with the instruction:

    info->lock->num1 = 5;

    and got a segmentation fault.

    I am using shared memory and need to put everything in one struct.

    Any help would be appreciated.
    Patricia Van Hise, Apr 5, 2004
    #1
    1. Advertising

  2. Patricia Van Hise wrote:
    > Is it possible to access a field of a struct which is a field of
    > another struct? Ex. struct subStr{
    > int num1;
    > int num2;
    > };
    > struct myStr {
    > int num3;
    > subStr *lock;


    I think you mean

    struct subStr *lock;

    > };
    >
    > struct myStr *info;
    >
    > I tried to store a value in num1 with the instruction:
    >
    > info->lock->num1 = 5;
    >
    > and got a segmentation fault.


    Did you assign the address of a struct myStr to info? If so, did you
    assign the address of a struct subStr to info->lock?

    --
    Russell Hanneken

    Remove the 'g' from my address to send me mail.
    Russell Hanneken, Apr 5, 2004
    #2
    1. Advertising

  3. Patricia Van Hise wrote:
    >struct subStr{
    > int num1;
    > int num2;
    >};
    >struct myStr {
    > int num3;
    > subStr *lock;


    should be: struct subStr *lock;
    does this compile? do not compile C code with a C++ compiler

    >};
    >
    >struct myStr *info;
    >
    >I tried to store a value in num1 with the instruction:
    >
    >info->lock->num1 = 5;
    >
    >and got a segmentation fault.


    How do you allocate info or lock? you did not provide the code, the
    access of num1 is correct but most probably info or lock are not
    pointing to a valid memory block, please provide the actual code you use.

    --
    John Tsiombikas (Nuclear / the Lab)

    http://thelab.demoscene.gr/nuclear/
    John Tsiombikas (Nuclear / the Lab), Apr 5, 2004
    #3
  4. Patricia  Van Hise

    Mike Wahler Guest

    "Patricia Van Hise" <> wrote in message
    news:...
    > Is it possible to access a field of a struct which is a field of
    > another struct?


    Yes.

    struct inner
    {
    int i;
    int j;
    };

    struct outer
    {
    int x;
    struct inner si;
    };

    struct outer out =
    {
    42,
    {25, 99}
    };

    printf("%d\n", out.si.j); /* prints 99 */



    > Ex. struct subStr{
    > int num1;
    > int num2;
    > };
    > struct myStr {
    > int num3;
    > subStr *lock;


    You have a syntax error. Should be:

    struct subStr *lock;

    BUT:
    This element is not a struct object, it's a pointer to
    a struct object.

    > };
    >
    > struct myStr *info;


    This is not a struct object, it's a pointer to a struct object.
    >
    > I tried to store a value in num1 with the instruction:
    >
    > info->lock->num1 = 5;
    >
    > and got a segmentation fault.


    You tried to store a value in memory not owned by your program.
    You need to actually create a type 'myStr' object in order
    to store data in it. And if you want to store anything in
    a type 'subStr' object, you need to create on of those too.

    struct subStr
    {
    int num1;
    int num2;
    };

    struct myStr
    {
    int num3;
    struct subStr lock;
    };

    struct myStr info = {0};
    info.lock.num1 = 5;

    >
    > I am using shared memory and need to put everything in one struct.


    See above.
    Why do you believe you need to use pointers?

    -Mike
    Mike Wahler, Apr 5, 2004
    #4
  5. Patricia  Van Hise

    Leor Zolman Guest

    On 4 Apr 2004 17:35:24 -0700, (Patricia Van Hise) wrote:

    [Note: there's nothing here in the way of analysis that hasn't been already
    posted, but perhaps you may find the complete program examples
    useful...there weren't /any/ responses yet when I started working on this,
    so I may as well not let the examples go to waste! ;-) ]

    >Is it possible to access a field of a struct which is a field of
    >another struct? Ex. struct subStr{
    > int num1;
    > int num2;
    > };
    > struct myStr {
    > int num3;
    > subStr *lock;


    Either you've been compiling with a C++ compiler, or there's a typedef line
    such as:
    typedef struct subStr subStr;
    you haven't shown us...


    > };
    >
    > struct myStr *info;
    >
    >I tried to store a value in num1 with the instruction:
    >
    > info->lock->num1 = 5;
    >
    >and got a segmentation fault.


    Sounds as if the value of lock within the struct myStr you're pointing to
    with info was never initialized...or perhaps info itself wasn't
    initialized, it is difficult to tell from what you've shown. If your
    structs are all being allocated dynamically, the proper sequence of events
    would look something like this:

    #include <stdio.h>
    #include <stdlib.h>

    struct subStr{
    int num1;
    int num2;
    };

    struct myStr {
    int num3;
    struct subStr *lock;
    };

    int main()
    {
    struct myStr *info;

    info = malloc(sizeof(struct myStr));
    info->lock = malloc(sizeof(struct subStr));
    /* ... */
    info->lock->num1 = 5;
    /* ... */
    free(info->lock);
    free(info);

    return 0;
    }


    >
    >I am using shared memory and need to put everything in one struct.


    Perhaps, then, you don't want to be using dynamic allocation after all, and
    just want to be using nested structs:

    #include <stdio.h>

    struct subStr{
    int num1;
    int num2;
    };

    struct myStr {
    int num3;
    struct subStr lock;
    };

    int main()
    {
    struct myStr info;

    /* ... */
    info.lock.num1 = 5;
    /* ... */

    return 0;
    }


    >
    >Any help would be appreciated.

    Hope that was some,
    -leor


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, Apr 5, 2004
    #5
  6. Patricia  Van Hise

    Al Bowers Guest

    Russell Hanneken wrote:

    > Patricia Van Hise wrote:
    >
    >> Is it possible to access a field of a struct which is a field of
    >> another struct? Ex. struct subStr{
    >> int num1;
    >> int num2;
    >> };
    >> struct myStr {
    >> int num3;
    >> subStr *lock;

    >
    >
    > I think you mean
    >
    > struct subStr *lock;
    >
    >> };
    >> struct
    >> myStr *info;
    >>
    >> I tried to store a value in num1 with the instruction:
    >>
    >> info->lock->num1 = 5;
    >>
    >> and got a segmentation fault.

    >
    >
    > Did you assign the address of a struct myStr to info? If so, did you
    > assign the address of a struct subStr to info->lock?
    >


    I suspect that the OP didn't and that is the source of the
    seg fault.

    The op can either define the struct with the substr object or leave
    it a pointer and either allocated storage or have it point to
    storage.

    Sample 1:
    #include <stdio.h>

    struct subStr
    {
    int num1;
    int num2;
    };

    struct myStr
    {
    int num3;
    struct subStr lock;
    };

    int main(void)
    {
    struct myStr a = { 4,{5,6}};

    printf("a.num3 = %d\n"
    "a.lock.num1 = %d\n"
    "a.lock.num2 = %d\n",
    a.num3, a.lock.num1, a.lock.num2);
    return 0;
    }


    Sample 2
    #include <stdio.h>

    struct subStr
    {
    int num1;
    int num2;
    };

    struct myStr
    {
    int num3;
    struct subStr *lock;
    };

    int main(void)
    {
    struct subStr sub = {5,6};
    struct myStr a = {3};

    a.lock = &sub;
    printf("a.num3 = %d\n"
    "a.lock->num1 = %d\n"
    "a.lock->num2 = %d\n",
    a.num3, a.lock->num1, a.lock->num2);
    return 0;
    }


    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
    Al Bowers, Apr 5, 2004
    #6
    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. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    548
    Chris Hauxwell
    Apr 27, 2004
  2. Paminu
    Replies:
    5
    Views:
    631
    Eric Sosman
    Oct 11, 2005
  3. Daniel Rudy
    Replies:
    15
    Views:
    1,376
    Keith Thompson
    Apr 10, 2006
  4. call_me_anything
    Replies:
    4
    Views:
    455
    Pete Becker
    Sep 30, 2007
  5. Tuan  Bui
    Replies:
    14
    Views:
    464
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page