What is a null pointer in C?
In C, a null pointer is a pointer that does not point to any memory location. It is a special constant value typically represented by the macro `NULL`. When a pointer is assigned the value `NULL`, it indicates that it does not point to a valid memory location.
Here's an example of using a null pointer:
int main() {
int *ptr = NULL; // Initializing a pointer with a null value
if (ptr == NULL) {
printf("The pointer is NULL\n");
} else {
printf("The pointer is not NULL\n");
}
return 0;
}
In this example, `ptr` is explicitly assigned the value `NULL`. Checking if a pointer is `NULL` is a common practice to ensure that the pointer is valid before dereferencing it to avoid potential runtime errors.
No comments