using namespace std;

struct node
    int info;
    node *left,*right;

int main()
    struct node *root;
    //root = (struct node*)malloc(sizeof(struct node));
    if(root==NULL) //cout<<root->info;
    cout<<root->info<< " "<<root->left<<" "<<root->right<<"\n";
    return 0;


why it is giving an error?

Recommended Answers

All 3 Replies

are you sure you posted in the right forum, the code is in C++ yet this is the C forum and you forgot to post the error messages
Now in a glance, 1 error I see is you should initialize the following variables inside the structure as follows

struct node *left; 
struct node *right

oh yes..my mistake..this is C forum.sorry
it gives run time error.

Member Avatar for MonsieurPointer

This is also incorrect:

> if(root==NULL) //cout<<root->info;
> cout<<root->info<< " "<<root->left<<" "<<root->right<<"\n";

You are trying to access the info member of root, which is NULL (due to your if statement); this will cause the program to crash.

It is best practice to always wrap statements in curly brackets to prevent unwanted behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.