×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous 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?
By IncludeHelp Last updated : March 10, 2024

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

C Pointer Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.