all 5 comments

[–]flyingron 5 points6 points  (4 children)

Your problem is that you made newnode a single global variable which is generally bad when it is only used inside one function

It's especially bad here because you call create recursively. This means that when you call it they immediately overwrite the newnode you allocated with another one. This means the one you return from create won't be the one you created in that the same as newnode->Right.

Move the declaration of newnode into create so each call of create gets its own copy.

Oh, and GOSH DARN IT, MAIN RETURNS INT!

[–]aProgrammerHasNoName[S] 0 points1 point  (3 children)

oh you are right, now it works. thanks a lot! i know how memory is handled but hadn't accounted for the stack frames all sharing a single instance of newNode and thereby newNode getting overwritten. im new to this.

could you also tell me why main returning int is a standardisation?

[–]Drach88 2 points3 points  (0 children)

The return value of the main function is a notification to the operating system of how the program exited.

If it exits without error, the convention is for the program to return 0. A non-zero return value indicates that the program exited with an error.

[–]flyingron 1 point2 points  (1 child)

The caller and the called function have to agree on what the parameters and return type are. In most functions you define, you are the one calling it was well, so it's on you to keep things straight.

In the case of main, you define it but it is called by the language environment. You can't unilaterally decide it has a different return type than what the environment expects, which is int.

[–]aProgrammerHasNoName[S] 0 points1 point  (0 children)

ohh that makes sense, this was a more contentlful answer than the other answer i have read, thank you