PattyB
PattyB

Reputation: 23

What is exactly being declared by typedef?

typedef struct nodetype node;
typedef node *link;

struct nodetype
{
 int  dat;
 link ptr;
};


typedef link *stack;

In the code above I am a little confused on what is all being defined. I understand what typedef does, but I am still wondering what exactly is being defined by typedef node *link; Is it defining a pointer to a node? Also I have the same question for the typedef link *stack;

Upvotes: 2

Views: 131

Answers (3)

John Bode
John Bode

Reputation: 123596

node is an alias for the type struct nodetype; link is an alias for the type node *, meaning it's also an alias for the type struct nodetype *.

stack is an object of type link *, which is the same as type node **, which is the same as type struct nodetype **.

This is a horrible example of how to use typedefs, precisely because of the confusion it is causing you. It's creating a bunch of different names for the same thing with no good reason, and it's hiding a pointer in one of typedef names which is almost always bad juju.

Upvotes: 1

pablo1977
pablo1977

Reputation: 4433

typedef defines an alias for a given type.
Its merit is to give some special meaning to the elements of your code.

Upvotes: 0

Yu Hao
Yu Hao

Reputation: 122503

The typedef lines are not creating anything. typedef is used to give new names for types, they are not declarations or definitions of variables, so nothing is really creating at this moment.

typedef node *link;

means link is another name for the type node *. Only when you use it like:

link link_varialbe;

A variable of type link (the same as node *) is created.

Upvotes: 2

Related Questions