Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,925 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,618 people online right now. Registration is fast and FREE... Join Now!




C++ help with basic pointers?

 
Reply to this topicStart new topic

C++ help with basic pointers?

ach
19 May, 2008 - 11:01 AM
Post #1

New D.I.C Head
*

Joined: 19 May, 2008
Posts: 3

I'm trying to write a binary tree with this as a tree:
QUOTE
struct node{
char val;
node *left;
node *right;
};

My variables are:

QUOTE
node *tree = new node;
node *root = new node;

root = NULL;
tree = NULL;

string equation;


equation is suppose to hold a simple equation like A+B

I want to read the equation into the node, like this:

QUOTE

root->left->val = equation[2]
root->val = equation[1]
root->right->val = equation[3]


So the tree will have + as the parent, and A and B as the children.

There aren't any compilation errors, but when I run the program, it crashes, and I did some cout statements, and found out there was an error setting the root->val stuff.

I've written a linklist with nodes where val was an integer, and it never crashed. why is it crashing when I try to set the value as a char?

This post has been edited by ach: 19 May, 2008 - 11:08 AM
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: C++ Help With Basic Pointers?
19 May, 2008 - 11:06 AM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
I see two possible issues.

Did you actually create new nodes for the left and right variables?

Other then that, arrays in C/C++ start off at 0. So, if the variable equation had "A+B" in it, this really isn't valid.
CODE

root->left->val = equation[2]
root->val = equation[1]
root->right->val = equation[3]


Assuming you actually allocated the left and right node, you will have this

root->left->val == 'B'
root->val == '+'
root->right->val == 0 // Null Terminator. Not what you want.

To be more verbose, here is what equation has
equation[0] == 'A'
equation[1] == '+'
equation[2] == 'B'
equation[3] == 0 // This might not be here. Compiler specific implementation
User is offlineProfile CardPM
+Quote Post

ach
RE: C++ Help With Basic Pointers?
19 May, 2008 - 11:12 AM
Post #3

New D.I.C Head
*

Joined: 19 May, 2008
Posts: 3

I fixed the string to start at 0 and it still crashes at run time.

Setting root's left and right values as new nodes doesn't work either.

Uhmm, this is my entire code if it will help

QUOTE

#include <iostream>
#include <string>
using namespace std;

struct node{
char val;
node *left;
node *right;
};

void main()
{
node *tree = new node;
node *root = new node;
root->right = new node;
root->left = new node;
root = NULL;
tree = NULL;

string equation;

cout<<"Enter your equation\n";
cin>>equation;

int i = 0;

root->left->val = equation[i];
root->val = equation[++i];
root->right->val = equation[++i];

}


PS: I'm using visual C++ by microsoft. Do you think that might have anything to do with it?

This post has been edited by ach: 19 May, 2008 - 11:14 AM
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: C++ Help With Basic Pointers?
19 May, 2008 - 11:18 AM
Post #4

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
Umm....

CODE

root = NULL;
tree = NULL;



You allocated the nodes, and then you set the variable that was pointing at them to 0 (NULL), so you are trying to read/write to address 0 which is invalid.
User is offlineProfile CardPM
+Quote Post

ach
RE: C++ Help With Basic Pointers?
19 May, 2008 - 12:32 PM
Post #5

New D.I.C Head
*

Joined: 19 May, 2008
Posts: 3

OH!!!

Oh my god.

Oh my god, I'm an idiot, thanks!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:16AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month