You can’t: you can only make a pointer null.
An array variable represents a contiguous block of memory containing values of a type that doesn’t need to be a pointer type. Since no pointers are involved, you can’t set it to NULL. Example:
- int a[100];
- a = NULL; // makes no sense! a is an array, not a pointer
- a[0] = NULL; // this makes no sense either! a[0] is an int.
When you declare an array of pointers, you can initialise the pointers in the array to zero, either using initialisation semantics, or explicitly with a loop:
- int *a[100] = { NULL }; // initialise array to NULL pointers
or:
- int *a[100];
- for (int i=0; i<100; i++) { a[i] = NULL; }
Finally, in C there is this trick where pointers can substitute for arrays. If you do a function call with an array, or assign an array to a pointer variable, then C will silently create a pointer to the first entry of the array for you. You can then use that pointer in place of the array:
- int a[100]; // a is an array
- int *b = a; // b is a pointer
- a[0] = 10; // we can use a to access the array...
- b[1] = 11; // but we can also use the pointer to a.
Of course, if what you have is really a pointer rather than the array itself, then you can set it to NULL:
- a = NULL; // This won't fly! a is an array
- b = NULL; // b is a pointer, so okay.
If you allocate an array on the heap with malloc, your handle to that array will always be a pointer variable. So:
- int *a = malloc(1000 * sizeof(int));
- // we cannot initialise the array during declaration,
- // so have to do it by hand:
- for (int i=0; i<1000; i++) a[i] = 0;
- a = NULL; // a is a pointer, so we can do this...
- // ... note, however, that we haven't freed the memory
- // on the heap, so setting a to NULL creates a memory leak.