We have seen that a pointer variable is a pointer to some other variable of the same data type. However, in some cases we may prefer to have null pointer which is a special pointer that does not point to any value.
NULL
POINTERS
We
have seen that a pointer variable is a pointer to some other variable of the
same data type. However, in some cases we may prefer to have null pointer which
is a special pointer that does not point to any value. This means that a null
pointer does not point to any valid memory address. To declare a null pointer you may use the predefined
constant NULL, which is defined in several standard header files including <stdio.h>, <stdlib.h>, and <string.h>. After including any of
these files in your program, write
int *ptr = NULL;
You
can always check whether a given pointer variable stores address of some
variable or contains a NULL by writing:
if (ptr = = NULL)
{
Statement block;
}
You
may also initialize a pointer as a null pointer by using a constant 0, as shown
below.
int
ptr;
ptr
= 0;
Programming Tip:
It
is a logical error to dereference a null pointer.
This
is a valid statement in C, as NULL
which is a preprocessor macro typically has the value, or replacement text, o.
However, to avoid ambiguity it is always better to use NULL to declare a null pointer.
A
function that returns pointer values can return a null pointer when it is
unable to perform its task.
Null
pointers are used in situations where one of the pointers in the program points
to different locations at different times. In such situations it is always
better to set it to a null pointer when it doesn't point anywhere valid, and to
test to see if it's a null pointer before using it.
Note
A
run time error is generated if you try to dereference a null pointer.
Programming in C: Unit III (b): Pointers : Tag: : with Example C Programs - Null Pointers
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Professional English II
HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Statistics and Numerical Methods
MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation
Engineering Graphics
GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Physics for Electrical Engineering
PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Basic Civil and Mechanical Engineering
BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Electric Circuit Analysis
EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Physics for Electronics Engineering
PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Electrical and Instrumentation Engineering
BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Circuit Analysis
EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Materials Science
PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Physics for Civil Engineering
PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Basic Electrical, Electronics and Instrumentation Engineering
BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Physics for Information Science
PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation