0

Just trying to get some code to work involving pointers, functions and recursion:

Node * root = malloc(sizeof(Node)); root->data = "1"; root->next = NULL; root->child = NULL; Node * G = NULL; BuildGraph(root, &G); printf("Root is: %d\n", root->data); Print(G, ">>"); // Custom Print function 

And BuildGraph:

void BuildGraph(Node * head, Node ** G) { if (head->child == NULL) { // No child printf("Head has no child! %d\n", head->data); Push(head, &G); Print(G, ">>"); return; } BuildGraph(head->child, &G); return; } 

And so when I run the program, my output goes like this:

Head has no child! 1 // printf in BuildGraph G: 1>> // Print(G, ">>") in BuildGraph Root is: 1 G is empty! // Print(G, ">>") in main 

Anyone know the reason G is not carrying over into main?

Thanks.

1 Answer 1

2

Within void BuildGraph(), BuildGraph(head->child, &G); should be BuildGraph(head->child, G);. No &, Likely the same with Push(head, &G);

Within your build function, G is a Node **. Outside in main(), G is a Node *.

Consider using a different and more expansive variable name than G within BuildGraph(). Maybe something like

void BuildGraph(Node * head, Node ** AddressG) { if (head->child == NULL) { // No child printf("Head has no child! %d\n", head->data); Push(head, AddressG); Print(AddressG, ">>"); return; } BuildGraph(head->child, AddressG); return; } 

I am confident that you compilation provided warning messages concerning this issue. If they did not recommend investigating how to turn them on.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your reply. I removed all the & characters like you said and still the list yields empty after calling BuildGraph. What I'm trying to achieve is a recursive way to populate G using the child and next structures starting from some node head. I've implemented Push/Pop using Node ** somelist without having to return the new list which is nice, and I'm looking for a way to do that here. Any more ideas?
Likely need to see 1) more of your code, 2) input data 3) compiler warning messages. That may need to be a re-vamped question.
Thanks for your reply. No compiler warning messages when I run it, and input data is root node. As for more code, there shouldn't be any need for it. The original post has outlined the problem. The output for G in the last line should be G: 1>> (in main) as it is in the BuildGraph loop, but instead it is giving me G is empty!.
I don't see where "is empty!" comes from. You must be getting warnings as root->data = "1"; implies data is char * yet printf("Head has no child! %d\n", head->data) implies data is int. Or your warnings are not turned on.
Don't worry about the Print function, that's a simple custom one that works fine.. The real problem is that G is NOT being updated in main when BuildGraph pushes to it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.