1

I'm really struggling to know what's going on with this struct in C++ :

typedef struct nod{
int nr;
nod* next;
}NOD; 

I know what a struct is and what purposes have, but I don't realize why "nod" and "NOD" is duplicate .And what's going on with the nod* inside the structure ?

This code is from chained lists.

if somebody can help me, I would appreciate it !!

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • 1
    nod and NOD is NOT duplicate. Just so you know, C/C++ is case-sensitive. nod* is pointing to structure itself. Read about self-referential structures. – SandBag_1996 Mar 24 '15 at 18:32
  • 2
    `NOD` is a `typedef` (alias) for `struct nod`. In C this can save you from having to type the word `struct` whenever you create instances of the type. C++ doesn't make you do that in the first place, so this sort of construct is usually redundant there. – dlf Mar 24 '15 at 18:35
  • NOD is a new type, declared as being `struct nod` - no black magic. – DrKoch Mar 24 '15 at 18:35
  • 1
    Relevant: [this Q&A](http://stackoverflow.com/q/612328/3549027) – dlf Mar 24 '15 at 18:40

5 Answers5

2

typedef a b creates an alias so that you can refer to a by writing b. So typedef struct nod {...} NOD allows you to refer to struct nod as NOD.

Without the typedef, i.e. struct nod {...};, you'd have to refer to the struct as struct nod in C code, while in C++ you could still refer to it as simply nod.

nod* next declares a member variable of the struct. The type of that variable is pointer to a nod, and it's name is next.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • I am not the down voter, and I cannot see anything you said that is technically wrong. The only thing I see perhaps is that the wording in the 3rd sentence is a little loose, and may have confused someone away from your intended thought. I reduced it in my mind to: _typedef struct a{...}A; allows the use of an alias_ ***A*** _in place of ***struct a***, allowing for more readable C code. This is a C technique as C++ allows the use of the struct name,_ ***a*** _in this case, without first typedefing it._ +1 in any case, for a complete answer. – ryyker Mar 24 '15 at 19:14
  • @ryyker Thanks, I modified it, hope it's clearer now. – Emil Laine Mar 24 '15 at 19:45
1

NOD and nod are not the same. NOD is a typedef, which basically means "the name of the struct". So you can define an object of it by typing:

NOD node;

However, nod* is different. It is a pointer of type nod, which is your struct. This can be used to chain together different struct objects and create a linked list like so:

Nod node1;
Nod node2;
Nod node3;

node1->next = node2;
node2->next = node3;
Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35
  • "NOD and nod are not the same." What's the difference? Both are names/identifiers and both name the same type. – MSalters Mar 24 '15 at 20:08
0

This defines a node in a linked data structure. Each node contains a pointer to another node (of the same type).

DrKoch
  • 9,556
  • 2
  • 34
  • 43
0

typedef define NOD as a synonym for the type struct node. So after that you can define a struct type node with NOD and NOD works as an allies of struct node. Now you can declare a new struct node with less verbiage -

NOD node1, node2;   

And the node* pointer inside the NOD is used to pointing an another struct node. Actually this struct node data structure is used to represent a node of a linked list.

Razib
  • 10,965
  • 11
  • 53
  • 80
  • The question is about C++, not C. The "verbiage saved" is just the e in `node`. The keyword `struct` is only needed in the struct definition itself. – MSalters Mar 24 '15 at 20:10
0

It is the same as :

typedef struct nod NOD;
struct nod {
  int nr;
  nod* next;
};

It is a linked list struct with struct member back referencing (pointing) its own type.

This is an old C convention (to neglect struct keyword) while in C++ the typedef is not quite needed here.

SwiftMango
  • 15,092
  • 13
  • 71
  • 136