473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 2205
-----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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
9
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
5
by: mdh | last post by:
Hi all, I have gone through the FAQ and done searches in the Comp.Lang and if I have missed it please let me have the ref. (Question generated in part by p119). Given char a;
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.