Home » C programs » C pointer programs

Making a valid pointer as NULL pointer in C

Here, we are going to learn how to make a valid pointer as a NULL pointer in C programming language?
Submitted by IncludeHelp, on November 14, 2018

Prerequisite: An Example of Null pointer in C

Any pointer that contains a valid memory address can be made as a NULL pointer by assigning 0.

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.

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.