473,796 Members | 2,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2036
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
1824
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 understand the format of the output it should provide. Here is the code below are some more comments: typedef struct tagGTime32 { union {
30
4276
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
5690
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 everything in a /*...*/ comment and appending the 'hail world' code. Is there an easier, and, let's say, more
2
3060
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 aspects of pointers. I'd appreciate if anybody could help me with the following problem: I tried to learn how to use malloc, free, and the * and & operators. I started with a few simple lines of code like:
16
3846
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
1486
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
7349
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 (condition) typedef char T; #else typedef short T;
8
2361
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', and the second demands that I prefix with struct? struct FooBar fb, *pfb?
15
2574
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 ! ) what is it supposed to do ?
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.