pointer question

J

jova

I'm try to make it short.
Why is my root ok on one pass bu,t then changes to some weird number after
additional calls to the insert function?



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.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I'm try to make it short.
Why is my root ok on one pass bu,t then changes to some weird number after
additional calls to the insert function?
[snip]
This is how I call the insert method


......

struct binaryNode tempNode = createNode();

insert(number, &tempNode);

OK, stop right there. You apparently are discussing C++ code

You've come to the wrong place. You want comp.lang.c++

This is comp.lang.c and we discuss the use of the standard C programming
language here. C++ is a different language, and the folks in comp.lang.c++
will be more than happy to help you.

[snip]

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFETY5xagVFX4UWr64RAoTkAKCAGeKPpDZ5Kxv2ZymFsMSkeyfd9gCg4qHK
1bpikGu/aXA98c8+TcDEFpQ=
=wUXn
-----END PGP SIGNATURE-----
 
I

Ian Collins

jova said:
I'm try to make it short.
Why is my root ok on one pass bu,t then changes to some weird number after
additional calls to the insert function?
To get any sensible help here, you'll have to post real code that can be
compiled and a better description of your problem.
 
I

Ian Collins

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I'm try to make it short.
Why is my root ok on one pass bu,t then changes to some weird number after
additional calls to the insert function?
[snip]
This is how I call the insert method


......

struct binaryNode tempNode = createNode();

insert(number, &tempNode);


OK, stop right there. You apparently are discussing C++ code

You've come to the wrong place. You want comp.lang.c++

This is comp.lang.c and we discuss the use of the standard C programming
language here. C++ is a different language, and the folks in comp.lang.c++
will be more than happy to help you.
Er, no, there isn't any evidence of C++. The OP would be boiled alive
for using malloc and struct in parameters on the C++ group.
 
J

jova

Lew Pitcher said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I'm try to make it short.
Why is my root ok on one pass bu,t then changes to some weird number
after
additional calls to the insert function?
[snip]
This is how I call the insert method


......

struct binaryNode tempNode = createNode();

insert(number, &tempNode);

OK, stop right there. You apparently are discussing C++ code

You've come to the wrong place. You want comp.lang.c++

This is comp.lang.c and we discuss the use of the standard C programming
language here. C++ is a different language, and the folks in comp.lang.c++
will be more than happy to help you.

[snip]

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFETY5xagVFX4UWr64RAoTkAKCAGeKPpDZ5Kxv2ZymFsMSkeyfd9gCg4qHK
1bpikGu/aXA98c8+TcDEFpQ=
=wUXn
-----END PGP SIGNATURE-----
No I'm not I'm doing C. I posted in a C++ board and they sent me over here.
To be honest I don't even think my code doesn't even have anything to do
with the language its something I'm not understanding. I program in Java
and this is my first C program using pointers so excuse me if I did
something that is not C style. Thank You.
 
J

jova

Ian Collins said:
To get any sensible help here, you'll have to post real code that can be
compiled and a better description of your problem.

I'm a little confused what more code do you need. You put everything that I
have there in C it should compile and run. Are you reading my whole post or
just skimming throught and looking for something wrong to complaing about?
 
T

TJW

Hello,

jova said:
I'm a little confused what more code do you need. You put everything that I
have there in C it should compile and run. Are you reading my whole post or
just skimming throught and looking for something wrong to complaing about?

When asking for help here, it is bet to supply the actual code (in
the smallest form possible) you are asking for help with.
The psuedo-code you posted cannot be compiled:
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 not just nitpicking. Specifically, you made the statement "Things work
as expected on the first call to insert() (when root == NULL)" then asked the
question "why does it not work on the next call to insert()?". But you did not
include whatever code you've written to handle the next call to your insert()
function, which makes answering your question in a meaningful way nearly
impossible.


Good Luck,
TJW
 
I

Ian Collins

jova said:
I'm a little confused what more code do you need. You put everything that I
have there in C it should compile and run. Are you reading my whole post or
just skimming throught and looking for something wrong to complaing about?
Attempting to help, people here often cut and paste posted code so they
can compile it and understand it. Picking out prose doesn't make this easy.

One tip - try working exclusively with pointers, yo pass them to most
functions, but don't return a pointer from createNode().
 
J

jova

Ian Collins said:
Attempting to help, people here often cut and paste posted code so they
can compile it and understand it. Picking out prose doesn't make this
easy.

One tip - try working exclusively with pointers, yo pass them to most
functions, but don't return a pointer from createNode().
Someone help me with it it was the createNode() function not returning a
pointer it was returning a struct thanks for the help. i just didn't want
to paste my whole project because it was calling things from other places
but thanks everyong for your help.
 
I

Ian Collins

jova said:
Someone help me with it it was the createNode() function not returning a
pointer it was returning a struct thanks for the help. i just didn't want
to paste my whole project because it was calling things from other places
but thanks everyong for your help.
Good to know you have an answer.

One more tip, set you news reader not to quote signatures (this is
normally the default).
 
K

Keith Thompson

Lew Pitcher said:
jova said:
I'm try to make it short.
Why is my root ok on one pass bu,t then changes to some weird number after
additional calls to the insert function?
[snip]
This is how I call the insert method


......

struct binaryNode tempNode = createNode();

insert(number, &tempNode);

OK, stop right there. You apparently are discussing C++ code

You've come to the wrong place. You want comp.lang.c++

This is comp.lang.c and we discuss the use of the standard C programming
language here. C++ is a different language, and the folks in comp.lang.c++
will be more than happy to help you.

I think you've mistaken the "&" in the function call for an "&" in a
function declaration. The line
insert(number, &tempNode);
is just a function call; the second argument is the address of tempNode.

If the function declaration looked something like this:
void insert(int number, struct binaryNode &tempNode);
that would be a C++ reference parameter.
 

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

Similar Threads

please help with pointer 3
Flattening linked list problem 4
passing struct pointer to function. 7
Lexical Analysis on C++ 1
struct and pointer question 33
Parse error? 3
Queue in C 25
simplebinary tree 7

Members online

Forum statistics

Threads
474,177
Messages
2,570,952
Members
47,506
Latest member
tomiy16522

Latest Threads

Top