473,473 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Binary Tree, makes pointer from integer without a cast

2 New Member
Hey Guys

I am consistently getting this error and cannot seem to find a solution for it...
I have pasted the relevant code beneath..
Any help will be appreciated...

Thanks

Expand|Select|Wrap|Line Numbers
  1. typedef struct node NODE;
  2.  
  3.  
  4. struct node {
  5.     int number;
  6.     int freq;
  7.     NODE *left, *right;
  8. };

Expand|Select|Wrap|Line Numbers
  1.     NODE *new_number,*old_number;

Expand|Select|Wrap|Line Numbers
  1.    if (input < new_number->number) {
  2.             if (new_number->left == NULL) {
  3.                 new_number->left = input;
  4.                 return 1;
  5.             }
  6.         old_number = old_number->left;
  7.         } else {
  8.             if (old_number->right == NULL) {
  9.                 old_number->right = input;
  10.                 return 1;
  11.             }
  12.             old_number = old_number->right;
  13.         }
  14.     }
  15.  
Sep 27 '08 #1
4 1482
Banfa
9,065 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1.    if (input < new_number->number) {
  2.             if (new_number->left == NULL) {
  3.                 new_number->left = input;
  4.                 return 1;
  5.             }
  6.         old_number = old_number->left;
  7.         } else {
  8.             if (old_number->right == NULL) {
  9.                 old_number->right = input;
  10.                 return 1;
  11.             }
  12.             old_number = old_number->right;
  13.         }
  14.     }
  15.  
This looks like tree traversal but it isn't clear why you are using 2 pointers new_number and old_number and how the interact.

Your described problem is these 2 lines
new_number->left = input;
old_number->right = input;
You are assigning a variable of type int to a pointer of type NODE *. This is not going to work, in the long run you will end up with segmentation faults or tree errors. Imagine trying to add an integer value of 0 to the tree.

You should be creating a new node, assigning the value to the member number and storing a pointer to the new node.
Sep 27 '08 #2
JosAH
11,448 Recognized Expert MVP
In lines #3 and #9 you're trying to set the left or right NODE pointer to 'input'
which is an int; hence the compiler diagnostic. You have to create a new NODE,
sets its data value to input and make the pointer point to the new node.

kind regards,

Jos
Sep 27 '08 #3
21st100
2 New Member
In lines #3 and #9 you're trying to set the left or right NODE pointer to 'input'
which is an int; hence the compiler diagnostic. You have to create a new NODE,
sets its data value to input and make the pointer point to the new node.

kind regards,

Jos
Hey Jos

You were Spot on...i cant believe i missed that...
I have corrected the error and it works like a charm..
Thanks for the help,

Cheers
Sep 27 '08 #4
JosAH
11,448 Recognized Expert MVP
Hey Jos

You were Spot on...i cant believe i missed that...
I have corrected the error and it works like a charm..
Thanks for the help,

Cheers
You're welcome; the credits need to be split between me and Banfa because
he was a split second faster than I was; 51% for me, 49% for Banfa ;-)

kind regards,

Jos
Sep 27 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example, I know the 18th through 21st byte is an integer...
14
by: Markus Dehmann | last post by:
The following if condition if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){ // ... } gives me a warning: assignment makes pointer from integer without a cast But only when I...
17
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using ,...
4
by: Dawn Minnis | last post by:
Hi When I compile my files I get the following: driver.c: In function `main': driver.c:49: warning: assignment makes integer from pointer without a cast driver.c:50: warning: assignment...
2
by: francescomoi | last post by:
Hi. I'm trying to compile this piece of source: ------------------------------------------- int id; while(row1 = mysql_fetch_row(rs1)) { id = atoi((int)row1);...
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
1
TMS
by: TMS | last post by:
I'm trying to write an address book that is based on a binary tree. I'm devloping in Visual C++ (I blew up my Ubuntu with the new dist, so no EMACS), starting with the basics: #ifndef...
2
by: aemado | last post by:
I am writing a program that will evaluate expressions using binary trees. Most of the code has been provided, I just have to write the code for the class functions as listed in the header file....
1
by: woods1313drew | last post by:
The following code in c+ gives me the warning assignment makes integer from pointer without a cast. destination is set as char destination to limit the input string to 10 characters. name is an...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.