2

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]);
            }
        }
    }
}
14
  • 2
    memset(Bucket, 0, sizeof(Bucket) / sizeof(Bucket[0])); Commented Apr 20, 2020 at 7:32
  • 4
    @SPlatten The third argument to memset is the number of bytes, so it should just be sizeof(Bucket) Commented Apr 20, 2020 at 7:35
  • 1
    @JoopEggen no, since Cxx (I don't remember... C99?) type array[N] = {0} is a common way to initialize all elements to 0. Commented Apr 20, 2020 at 7:37
  • 1
    Technically speaking, C standard doesn't guarantee that 0 == NULL. Now, I don't remember a single environment in which 0 != NULL, but in the past did exist. Commented Apr 20, 2020 at 7:42
  • 3
    @Aoerz That is not correct. If you divide by sizeof(Bucket[0]) you get the number of elements in the array. Useful for loops but useless for memset. memset does not care about elements. It takes the size of the whole memory areay. As you don't pass the size of an element, you would only get 10 bytes filled instead of 10 pointers. Commented Apr 20, 2020 at 7:49

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Is that comma in {NULL, } a typo o some synthax I don't know? Furthermore, In order ot complete your answer (and get also my UV ;) ), I suggest you to explain also {0} initializer.
@RobertoCaboni See edit (on the trailing comma). But I'm not sure what you mean by '"explain also {0} initializer" ... I thought I'd done that.
Bucket[] points to a location where there are 10 pointer of type struct Node * why are we making all of them NULL and not directly making Bucket as NULL and also what causes segmentation fault when we don't use NULL.
@UtkarshSingh Bucket is an array declared in automatic storage; referring to an array without a [] subscript is, effectively, referring to the address of the first element; this is determined by the compiler, from your code, and cannot be reassigned.
@UtkarshSingh You don't show how you use your Bucket array but, most likely, there is some code later on that checks for a 'valid' address in each array element before using it; typically, this would check for a non-NULL value, like: if (Bucket[n] != NULL) { ...; without the initialization, your array will be filled with arbitrary, unknown values.
|
3

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)

1 Comment

K&R originated C but are not authorities on what C is currently. The ISO/IEC C standard is widely accepted as the specification of C to use. Modern C includes designated initializers that can be used to initialize arbitrary elements, as in int x[10] = { [7] = 35; };.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.