Q:

Making a valid pointer as NULL pointer in C

belongs to collection: C pointers example programs

0

Making a valid pointer as NULL pointer in C

Example:

Here, firstly ptr is initialized by the address of num, so it is not a NULL pointer, after that, we are assigning 0 to the ptr, and then it will become a NULL pointer.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

#include <stdio.h>

int main(void) {
	int num = 10;
	int *ptr = &num;
	
	//we can also check with 0 instesd of NULL 
	if(ptr == NULL)
		printf("ptr: NULL\n");
	else
		printf("ptr: NOT NULL\n");

	//assigning 0 
	ptr = 0;
	if(ptr == NULL)
		printf("ptr: NULL\n");
	else
		printf("ptr: NOT NULL\n");

	return 0;
}

Output

ptr: NOT NULL
ptr: NULL

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Modify value stored in other variable using pointe... >>
<< An Example of Null pointer in C...