A typedef struct query

N

Neil McPhail

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.
 
V

Victor Nazarov

Neil said:
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;
};
 
E

Emmanuel Delahaye

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. */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top