Profile photo for Quora User

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:

  1. int a[100]; 
  2. a = NULL; // makes no sense! a is an array, not a pointer 
  3. 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:

  1. int *a[100] = { NULL }; // initialise array to NULL pointers 

or:

  1. int *a[100]; 
  2. 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:

  1. int a[100]; // a is an array 
  2. int *b = a; // b is a pointer 
  3.  
  4. a[0] = 10; // we can use a to access the array... 
  5. 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:

  1. a = NULL; // This won't fly! a is an array 
  2. 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:

  1. int *a = malloc(1000 * sizeof(int)); 
  2.  
  3. // we cannot initialise the array during declaration,  
  4. // so have to do it by hand: 
  5.  
  6. for (int i=0; i<1000; i++) a[i] = 0; 
  7.  
  8. a = NULL; // a is a pointer, so we can do this... 
  9. // ... note, however, that we haven't freed the memory 
  10. // on the heap, so setting a to NULL creates a memory leak. 
View 6 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025