Luis
Luis

Reputation: 127

Is it possible to create a struct inside the same uncreated struct?

Is the following declaration possible? If so, how do you access the elements inside the struct? It compiles but I cannot access the elements.

struct node
{
    int a;
    struct node *next;
};
struct node *node1;
int main()
{
    node1->a = 5; // I cannot perform this operation
    return 0;
}

Upvotes: 1

Views: 114

Answers (3)

moooeeeep
moooeeeep

Reputation: 32542

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:

Upvotes: 3

Niall
Niall

Reputation: 30604

Based on your MCVE, you don't create the node, you have an uninitialised pointer. The node* next seems to be a red herring to your current problem.

struct node
{
    int a;
    node *next; // seems to be a red herring to your current problem.
};

int main()
{
    node node1; // <-- for this demo, create it on the stack
    node1.a = 5;
    return 0;
}

Note; the node * is probably better understood as creating a pointer to a structure of the same type inside a struct (which is allowed), not so much a "struct in an uncreated struct".

Upvotes: 3

Nikita Smirnov
Nikita Smirnov

Reputation: 862

You cannot access fields because you created just a pointer, to a struct instance, but not the last one. Accessing elements thw way like this is an undefined behavior. To make it correct, you should write for example

struct node *node1;
int main()
{
    node1 = new node();
    node1->a = 5; // now ok
    node1->next = new node(); // again, before this we had an unitialized ptr.
    return 0;
}

The other question that it isn't a very efficient way in this context.

Upvotes: 1

Related Questions