please help with pointer

J

jova

I've posted this in the learn c-c++ group as well. Can some please help me
with why the rooot pointer variable keeps changing. First it is what is
supposed to be then it just changes. Here are some excerpts of my code.


I made root a global variable pointer.

struct binaryNode *root;

The first thing I did was set it NULL in the main.


root = NULL;

Then I use a integer and my root as a arguments in my insert method to
create the a Tree.

void insert(int x, struct binaryNode *node)

{


if(root==NULL)

{

root = node;

root -> value = x;

}

.......

}//end of insert I've cut the function just like you see here and the root
still changes


This is how I call the insert method


.......

struct binaryNode tempNode = createNode();

insert(number, &tempNode);

.......

And here his the createNode function.


struct binaryNode createNode()

{

struct binaryNode *t;

t = (malloc(sizeof(struct binaryNode)));

return *t;

}

Any Help is appreciated.
 
I

Ian Collins

jova said:
I've posted this in the learn c-c++ group as well. Can some please help me
with why the rooot pointer variable keeps changing. First it is what is
supposed to be then it just changes. Here are some excerpts of my code.
This looks like C, not C++, try comp.lang.c. Also cross-post, rather
than post to multiple groups.
 
N

Nerazzurri

you'd better rewrite the createNode function(),it should return a
pointer not a struct just as you did.
 
J

jova

Nerazzurri said:
you'd better rewrite the createNode function(),it should return a
pointer not a struct just as you did.

Thank You. That help very much. I'm jumping up and down. LOL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,511
Latest member
svareza

Latest Threads

Top