I think you need to review the basics of the language. So maybe a link to the booklist is in order:
To address your questions:
Here you define a type, i.e., a struct named node, as containing an object of type int
and an object of type pointer to an object of type node (struct node*
):
struct node
{
int a;
struct node *next;
};
Here you declare a global object of type pointer to an object of type node:
struct node *node1;
Note that pointers are invalid by default, that is, they don't point to an object automatically.
So you have a pointer, but you don't actually have an object of type node.
Therefore you are not allowed to dereference the pointer. It is forbidden to access the (arbitrary) memory location the pointer currently happens to point at.
int main()
{
node1->a = 5; // It is forbidden to perform this operation
return 0;
}
In order to fix this. You need to create an object and let the pointer point to it.
Example:
int main() {
node n; // create an object of type node
node1 = &n; // Assign the address of the object n to the pointer
node1->a = 5; // Now it's allowed to dereference the pointer
}
And finally:
Is it possible to create a struct inside the same uncreated struct?
You can have a type that contains pointers to objects of the same type. This can be useful to implement recursive data structures, e.g., linked lists or trees.
For further reading: