IMX
IMX

Reputation: 3652

Why pointer in typedef?

Seeing this struct

typedef struct Node{
    void *data;
    int pos;
    struct Node *prev;
    struct Node *next;
}*lptr;

I wonder why the typedef of Node is *lptr and not lptr. What difference does the pointer make?

Upvotes: 0

Views: 115

Answers (3)

David W
David W

Reputation: 10184

Because the definition (the typedef) is to a pointer type. Removing it makes lptr of type Node, * makes it a pointer to a Node.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

Although it is common to have two typedefs - one for the struct to avoid the tag, and one for the struct pointer to avoid the asterisk, like this

typedef struct Node{
    void *data;
    int pos;
    struct Node *prev;
    struct Node *next;
} Node;
typedef Node* lptr;

if the authors want to avoid writing an asterisk after lptr or Node, they could certainly typedef a pointer to struct Node.

Upvotes: 3

Klas Lindbäck
Klas Lindbäck

Reputation: 33273

The typedef is for a pointer type.

lptr is the type struct Node*

Upvotes: 1

Related Questions