Struct receiving the same struct as attribute

Asked

Viewed 81 times

1

Hello, how can I have a "recursive" struct? A struct that takes itself as one of the attributes.

typedef struct {
    int value;

    NODE[50];
} NODE;

In this case, the idea is that each Node has a vector with 50 child nodes. How can I do it? Thanks

  • You have to use a pointer whose type is the structure.

1 answer

1


The way you’re trying is not gonna make it because you’re trying to declare a structure where each knot will have 50 knots and each of these us will have other 50 knots and each of these us.. So I think you should just use a pointer instead of a vector.

typedef struct {
    int value;
    NODE *filho;
} NODE;

But I believe you need to control the 50 knots specific children of each knot, and there’s the problem. You could increase your struct to keep a list of items, but in each have the father and the amount that each has children, controlling it when adding a new item:

typedef struct {
    int indice; //por exemplo a posição deste item (1, 2, 3...) 
    int indice_pai; //a quem este elemento está referenciado
    int qtd_filhos; //inicializaria em 0 e receberia +1 a cada novo filho - serve para controlar os 50 máximos

    int value;
    NODE *filho;
} NODE;

It really gets a lot more complex to control if you need it, but I believe it’s a possible solution!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.