470,811 Members | 1,266 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

pointer question

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.

Apr 25 '06 #1
10 1966
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jova wrote:
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)

iD8DBQFETY5xagVFX4UWr64RAoTkAKCAGeKPpDZ5Kxv2ZymFsM Skeyfd9gCg4qHK
1bpikGu/aXA98c8+TcDEFpQ=
=wUXn
-----END PGP SIGNATURE-----
Apr 25 '06 #2
jova wrote:
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.

--
Ian Collins.
Apr 25 '06 #3
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jova wrote:
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.

--
Ian Collins.
Apr 25 '06 #4

"Lew Pitcher" <lp******@sympatico.ca> wrote in message
news:f7******************@news20.bellglobal.com...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jova wrote:
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)

iD8DBQFETY5xagVFX4UWr64RAoTkAKCAGeKPpDZ5Kxv2ZymFsM Skeyfd9gCg4qHK
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.
Apr 25 '06 #5

"Ian Collins" <ia******@hotmail.com> wrote in message
news:4b************@individual.net...
jova wrote:
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.

--
Ian Collins.


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?
Apr 25 '06 #6
TJW
Hello,

"jova" <jo**@nospam.net> writes:

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
Apr 25 '06 #7
jova wrote:
"Ian Collins" <ia******@hotmail.com> wrote in message
news:4b************@individual.net...
jova wrote:
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.

--
Ian Collins.

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().

--
Ian Collins.
Apr 25 '06 #8

"Ian Collins" <ia******@hotmail.com> wrote in message
news:4b************@individual.net...
jova wrote:
"Ian Collins" <ia******@hotmail.com> wrote in message
news:4b************@individual.net...
jova wrote:

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.

--
Ian Collins.

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().

--
Ian Collins.

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.
Apr 25 '06 #9
jova wrote:
"Ian Collins" <ia******@hotmail.com> wrote in message
news:4b************@individual.net...

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().

--
Ian Collins.


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

--
Ian Collins.
Apr 25 '06 #10
Lew Pitcher <lp******@sympatico.ca> writes:
jova wrote:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 25 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by Ben | last post: by
20 posts views Thread by __PPS__ | last post: by
204 posts views Thread by Alexei A. Frounze | last post: by
17 posts views Thread by Christian Wittrock | last post: by
9 posts views Thread by Cyron | last post: by
5 posts views Thread by mdh | last post: by
9 posts views Thread by subramanian100in | last post: by
2 posts views Thread by Giorgos Keramidas | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.