struct declaration (silly question)

Discussion in 'C Programming' started by Marcin Kasprzak, Mar 3, 2008.

  1. Hello Guys,
    Silly question - what is the most elegant way of compiling a code similar
    to this one?

    <code>
    typedef struct a {
    b_t *b;
    } a_t;

    typedef struct b {
    a_t *a;
    } b_t;

    int main(void) {

    return 0;
    }
    </code>

    my solution is to simply change type "b_t" to "void" but I'm not sure
    if that's the best way of dealing with this issue.

    Thanks,

    --
    Marcin Kasprzak
     
    Marcin Kasprzak, Mar 3, 2008
    #1
    1. Advertising

  2. Marcin Kasprzak

    Ben Pfaff Guest

    Marcin Kasprzak <> writes:

    > Silly question - what is the most elegant way of compiling a code similar
    > to this one?
    > [code for mutually referential structures]


    Refer to the C FAQ.

    1.14: I can't seem to define a linked list successfully. I tried

    typedef struct {
    char *item;
    NODEPTR next;
    } *NODEPTR;

    but the compiler gave me error messages. Can't a structure in C
    contain a pointer to itself?

    A: Structures in C can certainly contain pointers to themselves;
    the discussion and example in section 6.5 of K&R make this
    clear. The problem with the NODEPTR example is that the typedef
    has not been defined at the point where the "next" field is
    declared. To fix this code, first give the structure a tag
    ("struct node"). Then, declare the "next" field as a simple
    "struct node *", or disentangle the typedef declaration from the
    structure definition, or both. One corrected version would be

    struct node {
    char *item;
    struct node *next;
    };

    typedef struct node *NODEPTR;

    and there are at least three other equivalently correct ways of
    arranging it.

    A similar problem, with a similar solution, can arise when
    attempting to declare a pair of typedef'ed mutually referential
    structures.

    See also question 2.1.

    References: K&R1 Sec. 6.5 p. 101; K&R2 Sec. 6.5 p. 139; ISO
    Sec. 6.5.2, Sec. 6.5.2.3; H&S Sec. 5.6.1 pp. 132-3.
    --
    "I've been on the wagon now for more than a decade. Not a single goto
    in all that time. I just don't need them any more. I don't even use
    break or continue now, except on social occasions of course. And I
    don't get carried away." --Richard Heathfield
     
    Ben Pfaff, Mar 3, 2008
    #2
    1. Advertising

  3. Peter Nilsson, Mar 3, 2008
    #3
  4. On 3 Mar, 01:12, Marcin Kasprzak <> wrote:

    > Silly question - what is the most elegant way of compiling a code similar
    > to this one?
    >
    > <snip code>


    Others have mentioned the FAQ which deals with the necessary
    forward declaration, but as a point of style, I recommend:

    struct b;
    struct a {
    struct b *b;
    };

    struct b {
    struct a *a;
    };

    Note that your question is phrased poorly, since this has
    nothing to do with compiling, but with writing syntactically
    correct code that will compile. Also note that it is not
    necessary to include main() in order to compile. main()
    is only needed to link. (eg, 'gcc -c foo.c' will compile
    foo.c and generate foo.o, and no main() is necessary.)
     
    William Pursell, Mar 3, 2008
    #4
  5. On 3 Mar, 04:51, William Pursell <> wrote:
    > On 3 Mar, 01:12, Marcin Kasprzak <> wrote:
    >
    > > Silly question - what is the most elegant way of compiling a code similar
    > > to this one?

    >
    > > <snip code>

    >
    > Others have mentioned the FAQ which deals with the necessary
    > forward declaration, but as a point of style, I recommend:


    Ergg...no forward declaration necessary, of course. I
    was distracted by the typedef's and '_t's and mistook
    the question for a different FAQ.
     
    William Pursell, Mar 3, 2008
    #5
  6. Thanks guys for all your help,
    now it works fine.

    Once again thanks.

    --
    Marcin Kasprzak
     
    Marcin Kasprzak, Mar 3, 2008
    #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. Noah
    Replies:
    5
    Views:
    987
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,439
    Chris Fogelklou
    Apr 20, 2004
  3. Ehud Shapira
    Replies:
    20
    Views:
    1,180
    Ehud Shapira
    Jun 30, 2007
  4. slocum
    Replies:
    3
    Views:
    519
    slocum
    Apr 11, 2008
  5. heavyz

    Question about a struct declaration

    heavyz, Apr 16, 2008, in forum: C Programming
    Replies:
    17
    Views:
    423
    John Bode
    Apr 17, 2008
Loading...

Share This Page