C Library Macro -NULL
C Standard Library - <stddef.h>
Description
C Library MacroNULLis the value of a null pointer constant.
NULLis a macro that represents a null pointer constant. It is usually defined as((void*)0), which represents a null pointer pointing to memory address 0.- In C++,
NULLmay be defined as0or0L, so as to be compatible with integers0Compare.
Declaration
Below is a compiler-dependent declaration of the NULL macro.
#define NULL ((char *)0) 或 #define NULL 0L 或 #define NULL 0
Parameter
- NA
Return Value
- NA
Usage
NULLIt is used to indicate that a pointer does not point to any valid object or address. For example, when dynamic memory allocation fails, it is usually returnedNULL。NULLIt is also used to initialize pointer variables, indicating that they currently do not point to any valid address.
Example
The following example demonstrates the use of the NULL macro.
Example 1
#include <stddef.h>
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("file.txt", "r");
if( fp != NULL )
{
printf("Successfully opened file file.txt\n");
fclose(fp);
}
fp = fopen("nofile.txt", "r");
if( fp == NULL )
{
printf("Cannot open file nofile.txt\n");
}
return(0);
}
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("file.txt", "r");
if( fp != NULL )
{
printf("Successfully opened file file.txt\n");
fclose(fp);
}
fp = fopen("nofile.txt", "r");
if( fp == NULL )
{
printf("Cannot open file nofile.txt\n");
}
return(0);
}
Assume filefile.txtalready exists, butnofile.txtdoes not exist. Let us compile and run the above program, which will produce the following results:
成功打开文件 file.txt 不能打开文件 nofile.txt
dynamic memory allocation:
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int*)malloc(sizeof(int) * 10);
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use allocated memory
for (int i = 0; i < 10; i++) {
ptr[i] = i;
}
// Free allocated memory
free(ptr);
ptr = NULL; // Set the pointer to NULL
return 0;
}
#include <stdlib.h>
int main() {
int *ptr = (int*)malloc(sizeof(int) * 10);
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use allocated memory
for (int i = 0; i < 10; i++) {
ptr[i] = i;
}
// Free allocated memory
free(ptr);
ptr = NULL; // Set the pointer to NULL
return 0;
}
Function parameters:
Example
#include <stdio.h>
#include <string.h>
void printString(const char *str) {
if (str == NULL) {
printf("String is NULL\n");
} else {
printf("String: %s\n", str);
}
}
int main() {
char *string1 = "Hello, World!";
char *string2 = NULL;
printString(string1); // Output: String: Hello, World!
printString(string2); // Output: String is NULL
return 0;
}
#include <string.h>
void printString(const char *str) {
if (str == NULL) {
printf("String is NULL\n");
} else {
printf("String: %s\n", str);
}
}
int main() {
char *string1 = "Hello, World!";
char *string2 = NULL;
printString(string1); // Output: String: Hello, World!
printString(string2); // Output: String is NULL
return 0;
}
Notes
NULLIt is the standard way to represent a null pointer, avoiding the direct use of0or(void*)0, to improve code readability and maintainability.- When comparing pointers, you should use
NULL, rather than directly using0。 - In C++, you can use
nullptrinsteadNULL, to provide stronger type checking.
Related information
- In the C++11 standard, it introduced the
nullptrkeyword, as a more type-safe null pointer constant. NULLThe definition of the NULL macro may vary slightly depending on the compiler and standard library implementation, but its meaning and purpose remain the same.