1

What is the name of the struct below — node_t or node? Why is there any difference?

typedef struct node_t
{
    int data;
    struct node_t *right, *left;
}   node;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
noyuzi
  • 41
  • 3

1 Answers1

4

You are defining a name (node) for the struct node_t.

It allows you to create the structure using node myStruct instead of struct node_t myStruct.

Helio Santos
  • 6,606
  • 3
  • 25
  • 31
  • So just for complete understanding - node is related to the typedef? and allows me to call the struct without using the word struct? and the name of the struct is node_t? – noyuzi Jun 30 '14 at 14:06
  • it's the same as making a new type for an int - typedef int myint; but for a struct. – AnthonyLambert Jun 30 '14 at 14:18
  • Yes, yes, and no. The name of the struct (struct node_t) is node; – Jiminion Jun 30 '14 at 14:18
  • @noyuzi `typedef foo bar` declares the type `bar` as a synonym to `foo`, so `foo my_var` is equivalent to `bar my_var`. In this case, `foo`=`struct node_t` and `bar`=`node`. – humodz Jun 30 '14 at 14:56
  • Note that C++ has different rules and would not require the `typedef`; you could write `struct node { int data; node *right; node *left; };` and then use `node` as the name of a type (indeed, you've already used `node` as the name of the type inside the structure). – Jonathan Leffler Jun 30 '14 at 15:20