简体   繁体   中英

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;

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM