A typedef struct query

Discussion in 'C Programming' started by Neil McPhail, Jul 24, 2004.

  1. Neil McPhail

    Neil McPhail Guest

    I'm new to C (and Usenet) and have been using the dreaded Schildt's Complete
    C Reference. I appreciate this may not have been the best idea, so no need
    to point this out!

    On page 546 there is a mailing list program with a struct defined thus:

    struct address {
    char name[30];
    char street[40];
    char city[20];
    char state[3];
    char zip[11];
    struct address *next;
    struct address *prior;
    };

    The program is to demonstrate a doubly linked list, and works fine when
    compiled. I tried playing with it to tidy things up, and tried to make a
    typedef from the struct:

    typedef struct {
    char name[30];
    char street[40];
    char city[20];
    char state[3];
    char zip[11];
    address *next;
    address *prior;
    } address;

    This caused a compile time error. I assume this is because 'address' is
    referenced in the struct before the typedef is completed. I got round the
    problem by doing this:

    typedef struct address {
    char name[30];
    char street[40];
    char city[20];
    char state[3];
    char zip[11];
    struct address *next;
    struct address *prior;
    } address;

    ....which solves the problem. Is this the correct syntax or am I introducing
    undefined behaviour? Mr Schildt's book isn't much help!

    Thanks in advance.
    --
    Neil McPhail
     
    Neil McPhail, Jul 24, 2004
    #1
    1. Advertising

  2. Neil McPhail wrote:

    > I'm new to C (and Usenet) and have been using the dreaded Schildt's Complete
    > C Reference. I appreciate this may not have been the best idea, so no need
    > to point this out!
    >
    > On page 546 there is a mailing list program with a struct defined thus:
    >
    > struct address {
    > char name[30];
    > char street[40];
    > char city[20];
    > char state[3];
    > char zip[11];
    > struct address *next;
    > struct address *prior;
    > };
    >
    > The program is to demonstrate a doubly linked list, and works fine when
    > compiled. I tried playing with it to tidy things up, and tried to make a
    > typedef from the struct:
    >
    > typedef struct {
    > char name[30];
    > char street[40];
    > char city[20];
    > char state[3];
    > char zip[11];
    > address *next;
    > address *prior;
    > } address;
    >
    > This caused a compile time error. I assume this is because 'address' is
    > referenced in the struct before the typedef is completed. I got round the
    > problem by doing this:
    >
    > typedef struct address {
    > char name[30];
    > char street[40];
    > char city[20];
    > char state[3];
    > char zip[11];
    > struct address *next;
    > struct address *prior;
    > } address;
    >
    > ....which solves the problem. Is this the correct syntax or am I introducing
    > undefined behaviour? Mr Schildt's book isn't much help!
    >
    > Thanks in advance.

    There are two sets of names: one for structs and enums, and names for
    typedef objects (names of objects' types). this sets doesn't intersect.
    So your solution is correct.

    Besides you can use a better style (K&R use it):
    typedef struct address address;
    struct address {
    char name[30];
    char street[40];
    char city[20];
    char state[3];
    char zip[11];
    address *next;
    address *prior;
    };

    --
    vir
     
    Victor Nazarov, Jul 24, 2004
    #2
    1. Advertising

  3. Neil McPhail wrote on 24/07/04 :

    > struct address {
    > char name[30];
    > char street[40];
    > char city[20];
    > char state[3];
    > char zip[11];
    > struct address *next;
    > struct address *prior;
    > };
    >
    > The program is to demonstrate a doubly linked list, and works fine when
    > compiled.


    Ok.

    > I tried playing with it to tidy things up, and tried to make a
    > typedef from the struct:
    >
    > typedef struct {


    The alias is not yet defined.

    > char name[30];
    > char street[40];
    > char city[20];
    > char state[3];
    > char zip[11];
    > address *next;


    hence you have an error here ('address' unknown)

    > address *prior;
    > } address;
    >
    > This caused a compile time error. I assume this is because 'address' is
    > referenced in the struct before the typedef is completed.


    Absolutely.

    > I got round the
    > problem by doing this:
    >
    > typedef struct address {
    > char name[30];
    > char street[40];
    > char city[20];
    > char state[3];
    > char zip[11];
    > struct address *next;
    > struct address *prior;
    > } address;
    >
    > ...which solves the problem. Is this the correct syntax or am I introducing
    > undefined behaviour?


    It's correct, but there is another solution:

    /* separated incomplete alias definition */
    typedef struct address address;


    /* complete definition */
    struct address
    {
    char name[30];
    char street[40];
    char city[20];
    char state[3];
    char zip[11];
    address *next;
    address *prior;
    };

    That can be used like this (as far as only pointers are concerned) :

    Public interface :

    /* address.h */
    #ifndef H_ADDRESS
    #define H_ADDRESS

    /* separated incomplete alias definition */
    typedef struct address address;

    /* function prototypes */

    address* address_create (void);
    void address_delete (address* this);
    /* etc. */

    #endif /* guard */

    Implementation :

    /* address.c */

    #include "address.h"

    /* complete definition */
    struct address
    {
    char name[30];
    char street[40];
    char city[20];
    char state[3];
    char zip[11];
    address *next;
    address *prior;
    };

    /* public functions */
    address* address_create (void)
    {
    }

    void address_delete (address* this)
    {
    }

    /* etc. */

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Jul 24, 2004
    #3
    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. Replies:
    2
    Views:
    505
  2. DanielEKFA
    Replies:
    8
    Views:
    616
    DanielEKFA
    May 16, 2005
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  4. Steven T. Hatton
    Replies:
    2
    Views:
    681
  5. oor
    Replies:
    0
    Views:
    1,357
Loading...

Share This Page