>1- When we first define a pointer , is it automatically NULL?
It depends. A good rule of thumb is never to rely on default initialization. Always set your pointers to NULL or another predictable location.
>I could try this by writing a small program
You could, and that's a good reaction. However, this is a case where the result can be implementation-defined, which is the premise of the guideline above.
>2-Why is calloc bad?
calloc is bad? That's news to me.
>I could not figure out why would it be problem for us if calloc sets the pointer to 0 ( NULL ? YES ?)?
calloc doesn't set the pointer to NULL, it zero initializes the memory that the pointer points to. The problem with calloc is that zero initialization doesn't necessarily mean 0 for the type being initialized. For example:
Code:
int **p = calloc ( 10, sizeof *p );
if ( p[0] == NULL )
printf ( "Life is good!\n" );
This snippet is not required to print "Life is good!". You have to be careful with assumptions like that, and I imagine that's one of the reasons why you read that calloc is bad. The other is that the zero initialization can take extra time in an operation that's already slow.
>3-I still can not figure out the need for pointer when we write linked list or trees?
You don't need pointers for linked data structures. That's just the most common implementation. Here's a quickie linked list that doesn't use any pointers:
Code:
#include <stdio.h>
struct node {
int data;
int next;
};
int main ( void )
{
struct node list[] = {
{ 0, 5 },
{ 5, -1 },
{ 2, 4 },
{ 4, 1 },
{ 3, 3 },
{ 1, 2 }
};
int link = 0;
while ( link != -1 ) {
printf ( "%d -> ", list[link].data );
link = list[link].next;
}
printf ( "~\n" );
return 0;
}