No, really. Superficially, I know C but this is ... a new syntax for C99 probably:

struct node { int payload; int height; struct node *kid[2]; } dummy = {0, 0, {&dummy, &dummy}}, *nnil = &dummy; // internally, nnil is the new nul 

What does this mean ?:

  1. Create the type struct node node (the name of the type is still the struct node )
  2. typedef struct node dumy It turns out: declare a (global?) dummy variable containing, among other things, an array of two pointers to itself
  3. , => node* nnil = again a pointer to this dummy

I'm right? Is this syntax for this? C99? (emoticon "I'm terrified")

  • And the comments all finished off. In general, it is from here: rosettacode.org/wiki/AVL_tree/C - Rules
  • The most common "classic" syntax ... - PinkTux
  • Yes?? Well, I'm right about what's going on? - Rules
  • If you do not particularly find fault with it, then yes (correctly perceived), only instead of the term reference in C, the term pointer is used. - avp
  • 2
    @Rules: The name of the type here is the struct node , not the node , i.e. Your first item is correct exactly the opposite. - AnT

2 answers 2

 struct node { 

Announced structure

 } dummy 

Create a variable of type struct node

 = {0, 0, 

The first two fields of the dummy variable are zeros ( dummy.payload = dummy.height = 0 )

 {&dummy, &dummy}} 

The array elements kid (pointers) are assigned the address of the variable dummy ( dummy.kid[0] = dummy.kid[1] = &dummy )

 , *nnil 

Create another variable - a pointer to a variable of type struct dummy

 = &dummy; 

Assigned her the address of the variable dummy

Everything, no tricks, pure C without any frills.

  • HM thank you . Now I will know C. This is probably C99, - Rules
  • @Rules, no, this is a “clean” C. C99 (later standard) just at the end of the VladFromMoscow answer. - PinkTux
  • @PinkTux: Firstly, C99 is the same “pure” C as any other C. Secondly, it is impossible to say whether C99 is required here without seeing the context of the announcement. If this is a local ad, then formally it cannot be done in C89 / 90, because C89 / 90 will not allow &dummy inside the aggregate initializer. In C89 / 90, only compile time constants are allowed inside aggregate initializers. coliru.stacked-crooked.com/a/c5a6fa523f148963 - AnT
  • @AnT, this is not an error. Without -pedantic-errors everything is perfectly digested in -std=c90 and -std=c89 . And initialized: coliru.stacked-crooked.com/a/2489f3a854c6f24e - PinkTux
  • @PinkTux: These are errors . And the fact that "everything is digested" without -pedantic-errors means nothing. -pedantic-errors designed to detect such errors. I cited the requirement of the standard above. Another thing is that such an extension was quite popular in C89 / 90, but it is for suppressing such "popular extensions" that we have -pedantic-errors . - AnT

In this construction, the structure, the object of this structure and the pointer to the object of this structure are immediately declared.

To this ad

 struct node { int payload; int height; struct node *kid[2]; } dummy = {0, 0, {&dummy, &dummy}}, *nnil = &dummy; 

it was more understandable, you can break it into several ads. The original ad is equivalent to the following ad.

 struct node { int payload; int height; struct node *kid[2]; }; struct node dummy = {0, 0, {&dummy, &dummy}}; struct node *nnil = &dummy; 

That is, a structure is declared with the name struct node . Then an object of this structure is declared with the name dummy and its fields, as an object of the structure, are initialized with the corresponding values. To make this announcement even more understandable, you can even rewrite it in C99 as

 struct node dummy = { .payload = 0, .height = 0, .kid = { [0] = &dummy, [1] = &dummy }}; 

In this dummy object declaration, its data member kid , which is an array of pointers, is initialized with the address of the dummy object itself.

And, finally, in the third declaration, a pointer is declared with the name nnil to the dummy object

  • And do not even know whose answer is "accept" .. Or even delete my question? (It is a shame now to ask this and to choose from equivalent answers .. But do not be offended if - you also have a good answer).) - Rules
  • four
    @Rules Why delete a question? It is useful for other visitors to this site. :) - Vlad from Moscow
  • ABOUT! VB'shshnye chips in C! Cool, did not know :) - Qwertiy
  • one
    IMHO struct node dummy *nnil = &dummy; it is necessary to correct on struct node *nnil = &dummy; - andy.37