newprint newprint - 1 month ago 20
C Question

creating Typedef pointers to Typedef structs in C

Have a question about

typedef
in C.

I have defined struct:

typedef struct Node {
int data;
struct Node *nextptr;
} nodes;


How would I create
typedef
pointers to
struct
Node ??

Thanks !

Answer

You can typedef them at the same time:

typedef struct Node {
    int data;
    struct Node *nextptr;
} node, *node_ptr;

This is arguably hard to understand, but it has a lot to do with why C's declaration syntax works the way it does (i.e. why int* foo, bar; declares bar to be an int rather than an int*

Or you can build on your existing typedef:

typedef struct Node {
    int data;
    struct Node *nextptr;
} node;

typedef node* node_ptr;

Or you can do it from scratch, the same way that you'd typedef anything else:

typedef struct Node* node_ptr;