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


int main()

{
struct node *root;

    root = (struct node*)malloc(sizeof(struct node));

    root->info=NULL;
    root->left=NULL;
    root->right=NULL;

   if(root==NULL) printf("%d",root->info);
   return 0; 
}

why it doesnt print root->info?

Edited 4 Years Ago by shanki himanshu

I imagine it's not printing because root is not NULL. Change your if statement's test to

if (root != NULL) cout<<root->info;

cout<<root->info;

shouldn't that be printf(), this is the C forum after all

Edited 4 Years Ago by zeroliken

I imagine it's not printing because root is not NULL.

why root is not NULL? or how can i make it to NULL?

Edited 4 Years Ago by shanki himanshu

why root is not NULL? or how can i make it to NULL?

Why do you want it to be NULL? You just malloced some memory and put the address of it into root. Do you really want to loose that memory causing a memory leek?

Edited 4 Years Ago by WaltP

my actual problem is:
i make a linked list and i make the 'head' pointer global. then i created a insert() function. now in this function i have this condition: if(start==NULL). it works fine.

now i want to make a BST. and i am using pointer 'root' in main() (as shown in code i have posted). but here i cant compare it to NULL. why is so?

i make a linked list and i make the 'head' pointer global. then i created a insert() function. now in this function i have this condition: if(start==NULL). it works fine.

In linked list, you define *head=NULL at the time of declaration and YOU ARE NOT ALLOCATING ANY MEMORY FOR HEAD. It is just a pointer.
But, in this code, you are allocating memory for root. so, as soon as you allocate the memory, a memory block is given to root. something like 2217:08B6. (That's the reason why, your root is not null)
I agree with WaltP: when you are allocating memory to store something in root, then why do you want to make it NULL again?

Edited 4 Years Ago by Vish0203

and i am using pointer 'root' in main() (as shown in code i have posted). but here i cant compare it to NULL

huh? you can compare it to NULL, the if condition in your code won't be met since you allocated memory to it... as explained before

in a bst comparing the node to null is only used to check if it is allocated successfully, I don't see a reason to force it to be NULL

Edited 4 Years Ago by zeroliken

if i dont dont allocate memory here then it gives me run time error.

i did this:

struct node *root = NULL;
if(root==NULL) cout<< root->info;

it shows runtime error because you're storing value of info in it!!!
whereas in linkedlist "head" is just a pointer.. it doesn't store the value.

my head pointer is of type struct*. if head is pointing to first node of a linked list, its info part automatically stored the info of first node.
same goes for 'root' pointer of BST. i am trying to point it to root of BST by declaring in main(). the problem is i cant check it for NULL even if i dont allocate memory to it. whereas i can check for 'head' node for NULL in some other function but not in main . why ?

Edited 4 Years Ago by shanki himanshu

my head pointer is of type struct*. if head is pointing to first node of a linked list, its info part automatically stored the info of first node.

i think you should prefer a book about pointers. Please read it properly.
info value is already stored in the memory location using *curr or ***tmp** whaterver you use. You do not allocate memory for head. You allocate it for the temp variable you are using and after that you just point the head to that location.
pointer points.. It doesn't store any value unless you allocate a memory for it.

the problem is i cant check it for NULL even if i dont allocate memory to it

The reason why you are not able to check it is, if you don't allocate memory, you cannot use:

root->info=NULL;

because, for storing a value you need memory and if you remove the allocation line from your code, it becomes a processor fault as ther is no memory to store the info value(even if it is NULL or 0).

Just trace into your program. You'll understand why you are getting such problems

#include<iostream>
//#include<dos.h>
#include<conio.h>


using namespace std;

struct node
{
    int a;
    node *next;
};
struct node * abcd;
//void show(node *);
void insert()
{ 
    if(abcd==NULL)
     cout<<"abcd";
}
int main()

{
    struct node* abc;
    if(abc==NULL) cout<<"abc";
    insert();
    getch();
    return 0;
}

tell me why only 'abcd' is printed and not 'abc'?
(using dev-c++ compiler)

@vish

you are saying pointing head to temp doesnot store any value unless i allocate memory to it.

#include<iostream>


using namespace std;

struct node
{
    int a;
    node *next;
};
struct node * head;

void insert()
{ 
    struct node *temp;
    temp = (struct node*)malloc(sizeof(struct node));
    temp->a = 10;
    temp->next = NULL;
    head= temp;
    cout<<head->a;
}
int main()

{
    insert();
    getch();
    return 0;
}

since abcd is a global (external to main) pointer to node, so it is initialzed to 0 at compilation time. so this is null when u define that. thanks

Edited 4 Years Ago by I_m_rude

acc to you what should be the value of int a for abcd??

read my next post. posted mistakenly. regretted.

Edited 4 Years Ago by I_m_rude

acc to you what should be the value of int a for abcd??

there is no space allocated to abcd , so int a doesnt exist at that time. you will get semgmetaion fault if you try to access it.

it means that since no space is allocated for abcd so it is NULL? then what about abc? i didnot allocate memory for abc too?

I'll start from the top to avoid confusion.

tell me why only 'abcd' is printed and not 'abc'?

Because abcd is a global object and abc is a local object. Due to C's initialization rules, global objects are default initialized to the type's equivalent of zero, which in the case of a pointer is comparable to NULL.

This initialization rule doesn't apply to local objects, and those will contain whatever value was already in that location in memory, which may be invalid in any number of ways, especially for a pointer.

A good practice is to initialize everything explicitly:

struct node *abcd = NULL;

...

int main()
{
    struct node *abc = NULL;

    ...
}

However, you can get away with not initializing global objects (or objects with static duration in general) if the desired initialization value is the type's equivalent of zero:

struct node *abcd; // Default initialized to null

...

int main()
{
    struct node *abc = NULL; // Still needs explicit initialization

    ...
}

you are saying pointing head to temp doesnot store any value unless i allocate memory to it.

yes that's what I said. :P

This question has already been answered. Start a new discussion instead.