It is safe to pass a null pointer to free
, so if your array is initialized with null pointers, you can safely free all arr[i]
, assuming you only store pointers return by malloc()
and friends and do not free them elsewhere.
Define the array as
char *arr[3] = { NULL, NULL, NULL };
or simply
char *arr[3] = { 0 };
NULL
, 0
and ''
may be used to specify a null pointer, but it is advisable to follow a simple rule of clarity:
- use
NULL
for a null pointer,
- use
0
for a null integer,
- use
''
for a null byte in an array of char
, which is also called the null terminator.
And you can use = { 0 };
as an initializer for any C object: all elements and members will be intialized to the zero value of their type, except for unions where only the first member is initialized this way.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…