How can a C++ array be value-initialized?

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]();

Answers


  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.


Need Your Help

Export all contacts to VCF (vcard) file Windows phone

c# windows-phone vcard vcf

I want to export phone book contacts to single VCF file. I'm new to VCF file.

Flex/Bison EOF propagation from stdin vs a file

c bison lex flex-lexer

I have a scanner, parser and a main from which I create an executable via

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.