How can I have struct that contains a type of itself.
struct node { struct node *nodes[MAX]; int ID; };
struct node *node1, *node2;
node1 = (struct node*) malloc(sizeof(struct node));
node2 = (struct node*) malloc(sizeof(struct node));
node1->ID = 1;
node2->ID = 2;
node1->nodes[0] = node2;
node2->nodes[0] = node1;
There are no errors but the program doesn’t execute correctly.
EDIT: I’ve added more of my code.
FINAL: It was a mistake in my part that I created an infinite recursion. I’ll proceed to delete this threat. Sorry for your time spent.
That’s because you are storing an array of pointers to the struct. That’s quite different.
You can’t have the same struct inside itself. That would be an infinitely recursive definition.
Now, if you would show more of your program we may be able to help you understand why your program doesn’t run the way you expect. Chances are you have not initialised the pointers because of confusion over exactly what they are.
[edit] Now that you have posted some code, and ignoring that you haven’t said exactly what is going wrong, I expect that you are trying to iterate over the entire pointer list while examining your graph, but you never initialised it.
When you
malloc, the memory will be uninitialised. Standard practice in C is to usecallocinstead which will set all bytes to zero. Since you seem to be using thenodesarray as a list, you may want to add anum_edgesfield to the node and make a function to do a two-way join on two nodes.You could also test whether there is an edge from
atoblike this: