Null Pointer Assignment

It does make sense to assign an integer value to a pointer variable. An exception is an assignment of 0, which is sometimes used to indicate some special condition(Null pointer).

A macro is used to represent a null pointer. That macro goes under the name NULL. Thus, setting the value of a pointer using the NULL, as with an assignment statement such as ptr = NULL, tells that the pointer has become a null pointer. Similarly, as one can test the condition for an integer value as zero or not, like if (i == 0), as well we can test the condition for a null pointer using if (ptr == NULL) or you can even set a pointer to NULL to indicate that it’s no longer in use.

Let us see an example given below.

# include<stdio.h>
# define NULL 0
int *pi = NULL;
printf(“The value of pi is %u”, pi);

error null pointer assignment

when we initialize any pointer to null that mean not point to any thing now if we are trying to access mean use or try to put some value to it then the error null pointer assignment occur.

for example
int *p=null;

int mean p does not point to any other memory location
now if we write
*p=2;// or any integer
error null pointer assignment

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: