By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,081 Members | 861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,081 IT Pros & Developers. It's quick & easy.

A typedef struct query

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Nov 14 '05 #2

P: n/a
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"

Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.