Utkarsh Singh
Utkarsh Singh

Reputation: 49

How to initialize Array of pointers to NULL?

I was implementing Radix Sort which required array of pointer and to avoid segmentation fault I have to initialize it to NULL.

When I tried : struct Node *Bucket[10] = NULL

But it gives :error: invalid initializer

So , my teacher suggested : struct Node *Bucket[10] = {0}

So my question is what is the difference between {0} and NULL and I have also tried :

struct Node *Bucket[10] ;

     for(int i=0 ; i<10 ; i++)
     {
             Bucket[i] = NULL ;
     }

How {0} is same this for loop

Edit 1:

There is an additional question why are we making Bucket[0] .. Bucket[9] as NULL and how does it prevent segmentation fault.

void Radix_Sort(int *Arr)
{
    int max ;

    max = Max_element_in_array(Arr);

    struct Node *Bucket[10] = {0} ;

    for(int exp = 1 ; max / exp > 0 ; exp*=10)
    {
        int k=0 ;

        for(int i=begin ; i<end ; i++)
        {
            Append_a_linked_list(&Bucket[(Arr[i]/exp)%10],Arr[i]);
        }

        for(int j=0 ; j<10 ; j++)
        {
            while( Bucket[j] )
            {
                Arr[k++] = Delete_first_node(&Bucket[j]);
            }
        }
    }
}

Upvotes: 2

Views: 2822

Answers (2)

Adrian Mole
Adrian Mole

Reputation: 51894

The curly braces, { ... } are used for array initialization. When you declare an array, you can initialize its elements with syntax like this:

int a[3] = {1, 2, 3};

which sets the three members to, respectively, a[0] = 1, a[1] = 2 and a[2] = 3.

If the list contains less values than the array has elements, then all remaining elements are initialized to zero. From the link given above:

All array elements that are not initialized explicitly are initialized implicitly the same way as objects that have static storage duration.

Your pointer array has 10 elements but there is only one value in the initializer list, so all others are set to zero (as is the first element, explicitly).

Your use of NULL is actually not, in itself, the problem, as you could write (more clearly, IMHO):

struct Node *Bucket[10] = {NULL, };

as the NULL macro is generally defined like this:

#define NULL ((void *)0)

Note: The trailing comma I provided in the initializer list is otional but (again, IMHO) makes it clear(er) that you know there are more elements in the array, and that you are knowingly using the rule of 'implicit initialization to zero'. See here for a discussion: History of trailing comma in programming language grammars.

Feel free to ask for further clarification and/or explanation.

Upvotes: 6

Pierre Fran&#231;ois
Pierre Fran&#231;ois

Reputation: 6083

According to The C programming Language By Brian W. Kernighan and Dennis M. Ritchie, Section 4.9 Initialization: "There is no way to specify repetition of an initializer, nor to initialize an element in the middle of an array without supplying all the preceding values as well." These authors are the authority to define what is allowed in C.

So the correct solution seems to be:

struct Node *Bucket[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};

But... according to the same authors: "The symbolic constant NULL is often used in place of zero, as a mnemonic to indicate more clearly that this is a special value for a pointer. NULL is defined in <stdio.h>." (See Section: 5.4 Address Arithmetic)

So, the teacher is right when he proposes to initialize as:

struct Node *Bucket[10] = {0};

because "if there are fewer initializers for an array than the specified size, the others will be zero for external, static and automatic variables." (See Section: 4.9 Initialization)

Upvotes: 3

Related Questions