12
struct node
{
    int coef;

    int exp;

    struct node *link;
};

typedef struct node *NODE;
3
  • 6
    Please don't hide pointer types behind typedefs in real code though. Oct 29, 2011 at 20:03
  • what would be the better way to write this code.In the book structure is used in 3 to 4 ways...
    – Skr
    Oct 29, 2011 at 20:47
  • @Skr: be explicit and use struct node * instead of NODE. It's more readable, IMHO.
    – ninjalj
    Oct 29, 2011 at 21:05

5 Answers 5

18

It defines NODE as a synonym for the type struct node *, so when you'll be declaring a variable of type NODE you'll be actually declaring a pointer to struct node.

Personally, I don't think that such declaration is a good idea: you're "hiding a pointer" (which is almost always a bad idea), and, moreover, you are not highlighting this fact in any way into the new name.

3
  • 1
    +1 for the same reason. Naming it nodeptr instead of node would help, but then there's no reason to typedef it at all. Oct 29, 2011 at 20:11
  • 1
    "Don't hide pointer types behind a typedef" what do you mean by this
    – Skr
    Oct 29, 2011 at 20:43
  • 2
    He means that with that typedef you'll be working with a pointer type (struct node *) without making it evident to one that first reads your code (who just sees NODE, and probably assumes it's not a pointer). Oct 29, 2011 at 20:49
7

It makes NODE a typedef for a struct node *.

2
  • 1
    what would be a better way to write this code...retaining the same meaning.
    – Skr
    Oct 29, 2011 at 20:45
  • 3
    A better way would be to omit the typedef, and refer to the pointer type as struct node *. Oct 29, 2011 at 22:04
7

NODE becomes an alias for struct node*.


EDIT: Okay, for the comment (if I write my answer as comment, it would be too long and not formatted):

There's no different way to write this. Here, typedef is used just to create a synonym/alias for pointer to struct node.
An example for usage would be:

void f()
{
    // some code
    NODE p = NULL;
    // do something with p
    // for example use malloc and so on
    // like: p = malloc( sizeof( struct node ) );
    // and access like: p->coef = ..; p->expr = ..
    // do something with p and free the memory later (if malloc is used)
}

is the same as

void f()
{
    // some code
    struct node* p = NULL;
    // do something with p
}

Using NODE makes it just shorter (anyway, I wouldn't advise such typedef, as you're hiding, that it's a pointer, not a struct or other type, as mentioned in @Matteo Italia's answer).


The format, you're referring: "typedef struct{}type_name format" is something else. It's kind of a trick in C, to avoid writing struct keyword (as it's obligatory in C, and NOT in C++). So

typedef struct node
{
    //..
} NODE;

would make NODE alias for struct node. So, the same example as above:

void f()
{
    // some code
    NODE p;
    // do something with p
    // note that now p is automatically allocated, it's real struct
    // not a pointer. So you can access its members like:
    // p.coef or p.expr, etc.
}

is the same as

void f()
{
    // some code
    struct node p;
    // do something with p
}

NOTE that now, p is NOT a pointer, it's struct node.

4
  • can u write the same code in different way. how to write the same code in typedef struct{}type_name; format???
    – Skr
    Oct 29, 2011 at 21:30
  • "typedef struct{}type_name format" using this format how can i make NODE as a pointer.would it be like this typedef struct node*{}NODE;
    – Skr
    Oct 29, 2011 at 21:58
  • 1
    No, you should put the * after the definition of the struct, like this: typedef struct node { /* sth */ } *NODE;. You can even make 2 types like this (and I'd recommend that): typedef struct node { /* sth */ } NODE, *NODE_PTR, where NODE will be typedef for struct node and NODE_PTR will be typedef for struct node*. Oct 29, 2011 at 22:08
  • @Skr - if this answered your question, you might want to accept it :) Nov 24, 2011 at 16:24
0

simply tells you can create pointer of node type using only NODE every time instead of writting struct node * everytime

-4

what does typedef struct node *NODE indicate?

UPPERCASE IS NO GOOD

Reserve ALL UPPERCASE identifiers for MACROS.

1
  • Also, I don't think it's a good idea to let NODE contains *
    – Peter Lee
    Jun 26, 2013 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.