Null pointer in C
In this program, you will learn the concept of null pointer and get the example code of null pointer in c programming.
What is Null Pointer in C
A null pointer is a special type of pointer in C and C++ that points to nothing, not even itself. It represents the absence of any valid pointer.
Example
The following is an example of declaring and using a null pointers in C:
// Declaration of null pointer
int *ptr = NULL;
// Check if pointer is null
if (ptr == NULL)
{
printf(“Pointer is null\n”);
}
Size of Null Pointer in C
Size of a null pointer is depends on the architecture and compiler of the system.
On 32 bit system, A null pointer usually has 4 bytes in size.
On 64 bit system, A null pointer usually has 8 bytes in size.
To check the size of a null pointer in c programming, use the ‘sizeof’ operator. Run the following program code to check the size of a null pointer in c programming.
1 2 3 4 5 6 7 | #include <stdio.h> int main() { int *ptr = NULL; printf("Size of a null pointer is : %zu bytes\n", sizeof(ptr)); return 0; } |
Above program will print the size of a null pointer based on the compiler and system you are using.
Null Pointer in C example
1 2 3 4 5 6 7 | #include<stdio.h> int main() { int *ptr=NULL; printf("The value of ptr is : %x\n",ptr); return 0; } |
Output
The value of ptr is : 0
Check out our other C programming Examples