Home » C programs » C pointer programs

An Example of Null pointer in C

C language NULL pointer: Here, we are going to learn about the NULL pointer in C programming language with Example.
Submitted by IncludeHelp, on November 14, 2018

NULL pointer

The word "NULL" is a constant in C language and its value is 0. In case with the pointers - if any pointer does not contain a valid memory address or any pointer is uninitialized, known as "NULL pointer". We can also assign 0 (or NULL) to make a pointer as "NULL pointer".

Example:

In this example, there are 3 integer pointers ptr1, ptr2 and ptr3. ptr1 is initialized with the address of the integer variable num, thus ptr1 contains a valid memory address. ptr2 is uninitialized and ptr3 assigned 0. Thus, ptr2 and ptr3 are the NULL pointers.

Program:

#include <stdio.h>

int main(void) {
	int num = 10;

	int *ptr1 = &num;
	int *ptr2;
	int *ptr3=0;

	if(ptr1 == 0)
		printf("ptr1: NULL\n");
	else
		printf("ptr1: NOT NULL\n");

	if(ptr2 == 0)
		printf("ptr2: NULL\n");
	else
		printf("ptr2: NOT NULL\n");

	if(ptr3 == 0)
		printf("ptr3: NULL\n");
	else
		printf("ptr3: NOT NULL\n");

	return 0;
}

Output

ptr1: NOT NULL 
ptr2: NULL 
ptr3: 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.