简体   繁体   中英

defining typedef for a struct for 2 times

I saw in a code such a thing like:

struct listnode{
char data;
struct listnode *nextptr;}

typedef struct listnode LISTNODE;

typedef LISTNODE *listnodeptr;

so why did he use typedef for 2 times???

The first typedef declares an alias for the structure itself

typedef struct listnode LISTNODE;
        ^^^^^^^^^^^^^^^

The second typedef declares an alias for pointer to object of the structure type.

typedef LISTNODE * listnodeptr;
        ^^^^^^^^^^

It is matter of style whether to use several typedefs of just one.

All these three declarations could be combined in one declaration

typedef struct listnode{
char data;
struct listnode *nextptr;
} LISTNODE, *listnodeptr;

Two typedef s here are not repetitive, they alias two different types.

  • typedef struct listnode LISTNODE; creates the type LISTNODE which is an alias of struct listnode
  • typedef LISTNODE *listnodeptr; creates the type listnodeptr which is an alias of LISTNODE * , ie, struct listnode *

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