edited by
935 views

1 Answer

Best answer
0 votes
0 votes
char *p; //Till here no problem
*p = 'a'; //Till is problem
printf("%c",*p); //This is problem

By declaring char pointer we have not initialized so it must be pointing to some random address.

Now if we try to access a random location we will end up with undefined behaviour as random address may work fine also but what if the random address belongs to some other process address space then we will get a segmentation fault. So its good practice to initialize the pointers.

Also we won't have compile time error rather run time error.

selected by

Related questions

1 votes
1 votes
2 answers
2
0 votes
0 votes
1 answer
4
UK asked Mar 25, 2016
667 views
What is the difference between struct node* head= NULL and struct node* head= (struct node*)NULL ?