If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C language, the NULL macro is used to represent a null pointer, which is a pointer that does not point to any valid memory location. The NULL macro is defined in the <stdio.h> and <stddef.h> headers and is usually set to 0.
Here is an example of a null pointer:
Copy code
#include <stdio.h>
int main() {
int* p = NULL; // p is a null pointer
printf("%p\n", p); // prints 0
return 0;
}
In this example, the pointer p
is initialized with the value NULL, meaning it does not point to any valid memory location. Attempting to dereference a null pointer will result in undefined behavior, so it's important to check if a pointer is null before dereferencing it.
Copy code
#include <stdio.h>
int main() {
int* p = NULL; // p is a null pointer
if (p != NULL) {
printf("%d\n", *p);
} else {
printf("p is a null pointer\n");
}
return 0;
}
The NULL macro can also be used to initialize a variable of any pointer type to a null pointer.
Copy code
#include <stdio.h>
int main() {
int* p1 = NULL;
char* p2 = NULL;
double* p3 = NULL;
return 0;
}
It's also worth noting that, the NULL pointer is not the same as an uninitialized pointer, an uninitialized pointer has an indeterminate value and it could be any value
Comments: 0