473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A typedef struct query

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
2 2007
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Lorn | last post by:
I was hoping some of you might be able to help me understand the following code defining a typedef for a time query to the WINAPI. I understand the basics of what's going on, I just don't...
30
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
4
by: James Brown | last post by:
I can't find this covered in the FAQ: typedef int foo; typedef struct foo { int a, b, c; } bar;
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
8
by: Ronny Mandal | last post by:
Consider these two: Typedef struct { int foo, bar } FooBar; struct FooBar { int foo, bar }; What woul be the only difference here; just that I can create new instances by 'Foobar fb, *pfb',...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.