In c ++, elements are assigned a zero in the structure, and when I try to do it in C, I get an error, how can I fix / replace it?

struct example {example *p=NULL;} 

And one more example from c ++, which does not work on C when calling a function, gives an error on "* &"

 void function(spis *&h); 
  • one
    C is not C ++. If you can't do something in C that you can do in C ++, then this is not surprising. - AnT 2:46 pm

1 answer 1

In C, unlike C ++, you cannot specify initializers in a structure declaration.

Therefore, this structure declaration

 struct example {example *p=NULL;}; 

will not compile. Moreover, in C, this declaration is also incorrect for another reason: the name of example not the name of a structure. It must be preceded by the keyword struct . for example

 struct example { struct example *p; }; 

In principle, there is no need to use an initializer inside a structure declaration. You can always initialize the corresponding fields when creating a structure object or assign values ​​to them after creating a structure object. For example,

include

In C :

 struct example { struct example *p; }; int main( void ) { struct example list = { NULL }; } 

Or

 #include <stdlib.h> struct example { struct example *p; }; int main( void ) { struct example *list = malloc( sizeof( struct example ) ); list->p = NULL; //... free( list ); } 

As for this ad

 void function(spis *&h); 

then in C ++ the parameter is a reference type to a spis * type spis * . There are no reference types in C. For greater understanding, this declaration can be rewritten as follows in C ++:

 typedef spis * T; void function(T &h ); 

that is, the parameter h is a link.

To pass an object to a function by reference in C, you need to pass it through a pointer to an object.

That is, the above function should be declared as follows.

 void function(spis **h); 

Such a function will work in C and in C ++.