using typedef for structures in C

R

Ravikant

Hello,
Here I have written two types of declaring a structure.
Type:1
typedef struct node
{
1. U32 age;
2. char name[100];
3. struct node *rlink;
4. struct node *llink;
}Data_node;
Type:2
typedef struct
{
1. U32 age;
2. char name[100];
3. ..............;
4. ..............;
}Data_node;

Now I am not knowing what should be written in second type of
structure declaration at lines 3 and 4.


Please let me know about it.Thank you.
bye
Ravi
 
H

Horst Kraemer

Hello,
Here I have written two types of declaring a structure.
Type:1
typedef struct node
{
1. U32 age;
2. char name[100];
3. struct node *rlink;
4. struct node *llink;
}Data_node;

An equivalent variation

typedef
struct node Data_node;

struct node
{
U32 age;
char name[100];
Data_node *rlink;
Data_node *llink;
};


Type:2
typedef struct
{
1. U32 age;
2. char name[100];
3. ..............;
4. ..............;
}Data_node;

Now I am not knowing what should be written in second type of
structure declaration at lines 3 and 4.

Impossible. If you define a struct type without tag name (like 'node'
in your first example), i.e. an unnamed struct type, then you can't
define a pointer to this struct type inside the struct because the
struct has no name to refer to and its typedef name 'Date_node' is
only introduced textually *after* the complete declaration.
 
R

Richard Bos

Type:1
typedef struct node
{
1. U32 age;
2. char name[100];
3. struct node *rlink;
4. struct node *llink;
}Data_node;
Type:2
typedef struct
{
1. U32 age;
2. char name[100];
3. ..............;
4. ..............;
}Data_node;

Now I am not knowing what should be written in second type of
structure declaration at lines 3 and 4.

You can't do it like that. You can't use a Data_node *, because
Data_node hasn't been written yet. You can't use a struct <something> *,
because you've removed the <something>. So you need a trick. This one,
for example:

typedef Data_node *Data_node_ptr;
typedef struct {
U32 age; /* BTW, it's probably better not to use an explicitly sized
integer here, unless you're sure that it'll always be
large enough. And if it's a human age, a simple char
would suffice...*/
char name[100];
Data_node_ptr rlink;
Data_node_ptr llink;
} Data_node;

See also the FAQ, posted just this morning.

Richard
 
S

Sean Kenwrick

Richard Bos said:
Type:1
typedef struct node
{
1. U32 age;
2. char name[100];
3. struct node *rlink;
4. struct node *llink;
}Data_node;
Type:2
typedef struct
{
1. U32 age;
2. char name[100];
3. ..............;
4. ..............;
}Data_node;

Now I am not knowing what should be written in second type of
structure declaration at lines 3 and 4.

You can't do it like that. You can't use a Data_node *, because
Data_node hasn't been written yet. You can't use a struct <something> *,
because you've removed the <something>. So you need a trick. This one,
for example:

typedef Data_node *Data_node_ptr;
typedef struct {
U32 age; /* BTW, it's probably better not to use an explicitly sized
integer here, unless you're sure that it'll always be
large enough. And if it's a human age, a simple char
would suffice...*/


Hey, I plan to live to around 129 years old which could crash his program if
he followed your advice above... :)

Sean
 
C

CBFalconer

Ravikant said:
Here I have written two types of declaring a structure.
Type:1
typedef struct node
{
1. U32 age;
2. char name[100];
3. struct node *rlink;
4. struct node *llink;
}Data_node;
Type:2
typedef struct
{
1. U32 age;
2. char name[100];
3. ..............;
4. ..............;
}Data_node;

Now I am not knowing what should be written in second type of
structure declaration at lines 3 and 4.

Yup, that is a problem. I always have difficulties referring to
nameless entities. With that in mind, which form do you think you
should actually use?
 
M

Mark A. Odell

Hey, I plan to live to around 129 years old which could crash his
program if he followed your advice above... :)

Plain char can be unsigned but if signed, then unsigned char should do you
fine.
 
D

Dave Thompson

(e-mail address removed) (Ravikant) wrote:
(selfreferential struct with and without tag)
You can't do it like that. You can't use a Data_node *, because
Data_node hasn't been written yet. You can't use a struct <something> *,
because you've removed the <something>. So you need a trick. This one,
for example:

typedef Data_node *Data_node_ptr;
typedef struct { [using Data_node_ptr]

Nope. You can have, or typedef, a pointer to incomplete type, but not
to completely unknown typename as in Pascal or Ada. There's no way to
make a struct type selfreferential without giving it a tag.

- David.Thompson1 at worldnet.att.net
 

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
474,139
Messages
2,570,805
Members
47,352
Latest member
DianeKulik

Latest Threads

Top