Here is the code
struct stack {
int item;
struct stack *next=NULL;
};
I want to make the pointer initially point to null but this shows error
error:expected ':' , ',' , ';' , '}' or 'attribute' before'=' token
The fragment posted does not define an object, it defines a type.
In C you cannot specify the default value of a member in a type definition. You can specify the values of members in an initializer:
struct stack {
int item;
struct stack *next;
};
struct stack my_stack = { 0, NULL };
If you omit the last member in the initializer, it will be initialized to the zero of its type, NULL for a pointer:
struct stack my_stack = { 0 }; // my_stack.next is NULL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With