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]);
}
}
}
}
memsetis the number of bytes, so it should just besizeof(Bucket)type array[N] = {0}is a common way to initialize all elements to 0.0 == NULL. Now, I don't remember a single environment in which0 != NULL, but in the past did exist.sizeof(Bucket[0])you get the number of elements in the array. Useful for loops but useless formemset.memsetdoes 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.