struct with struct

This is a discussion on struct with struct within the C Programming forums, part of the General Programming Boards category; Code: letdef struct list_tag * list; struct list_tag { enum { ..... } kind; struct { int x; struct { ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    36

    struct with struct

    Code:
    letdef struct list_tag * list;
    
    struct list_tag {
        enum {
           .....
        } kind;
        struct {
            int x;
            struct {
                    int y;
            } nest1;
        } nest0;   
    }
    with the declaration above is it ok to say the following? if not how can i do it??
    Code:
    list one;
    one = (list_tag *) malloc(sizeof(*  one));
    one.nest0.nest1.y = 2

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > one = (list_tag *) malloc(sizeof(* one));
    Don't cast malloc in C - see the FAQ.

    > one.nest0.nest1.y = 2
    one is a pointer, so you need the -> operator.
    The rest is correct
    one->nest0.nest1.y = 2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    it should be
    Code:
    one->nest0->nest1->y=2;
    as one is a pointer of type list

    s.s.harish

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssharish
    it should be
    Code:
    one->nest0->nest1->y=2;
    as one is a pointer of type list

    s.s.harish
    No it shouldn't. nest0 is not a pointer, neither is nest1.

    Oh, and to the OP, you're missing a ; on your last braces.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    oops.. sorry, you are right quzah,

    thax

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21