0

1) In C++, is providing the initializer list {} the same as {0}? Will the statements:

int x[10]={};

int x[10]={0};

both produce the same array with all elements initialized to 0?

2) On systems/compilers where NULL is not 0, do default-value initializations of arrays of pointers set the elements to NULL or to 0? Which of the following statements should/can be used?

int *x[10]={NULL};

int *x[10]={0};

int *x[10]={};

How about new value-initializers using empty parentheses -- do they use NULL or 0 as the initializer?

int **x=new int*[10]();

Brad Fox
  • 685
  • 6
  • 19
user553702
  • 2,819
  • 5
  • 23
  • 27

1 Answers1

5
  1. Yes. Both of those initialisers are equivalent in functionality. The difference is that the second one explicitly initialises the first element to 0, and implicitly value-initialises all remaining elements in the array (which in the case of int, means setting them to 0).

  2. NULL is 0 on all compilers that conform to the C++ standard. In this instance, all three are the same in terms of functionality. NULL is a macro that expands to 0, so the first two are identical in the eyes of the compiler. I'm not sure what the deal will be with C++11, but the value-initialisation of a pointer means setting that pointer to NULL.

dreamlax
  • 93,976
  • 29
  • 161
  • 209