is this form of intializing an array to all 0s
char myarray[ARRAY_SIZE] = {0} supported by all compilers? ,
if so, is there similar syntax to other types? for example
bool myBoolArray[ARRAY_SIZE] = {false}
Yes, this form of initialization is supported by all C++ compilers. It is a part of C++ language. In fact, it is an idiom that came to C++ from C language. In C language = { 0 } is an idiomatic universal zero-initializer. This is also almost the case in C++.
Since this initalizer is universal, for bool array you don't really need a different "syntax". 0 works as an initializer for bool type as well, so
bool myBoolArray[ARRAY_SIZE] = { 0 };
is guaranteed to initialize the entire array with false. As well as
char* myPtrArray[ARRAY_SIZE] = { 0 };
in guaranteed to initialize the whole array with null-pointers of type char *.
If you believe it improves readability, you can certainly use
bool myBoolArray[ARRAY_SIZE] = { false };
char* myPtrArray[ARRAY_SIZE] = { nullptr };
but the point is that = { 0 } variant gives you exactly the same result.
However, in C++ = { 0 } might not work for all types, like enum types, for example, which cannot be initialized with integral 0. But C++ supports the shorter form
T myArray[ARRAY_SIZE] = {};
i.e. just an empty pair of {}. This will default-initialize an array of any type (assuming the elements allow default initialization), which means that for basic (scalar) types the entire array will be properly zero-initialized.
bool myBoolArray[ARRAY_SIZE] = { false }; the array is all initialized to false, but if i write bool myBoolArray[ARRAY_SIZE] = { true }; only the first element is set to true, while everything else is false. Any explaination?bool array, the rest is zero-initialized for you. That's just the way it is.bool myBoolArray[ARRAY_SIZE];, the array rightfully contains random values, therefore I wouldn't have expected that an initialization could return an array with different values in it. That's all.Note that the '=' is optional in C++11 universal initialization syntax, and it is generally considered better style to write :
char myarray[ARRAY_SIZE] {0}
char myarray[ARRAY_SIZE] {0}? Init the each element of myarray for ARRAY_SIZE times?0, default init others (which in this case is zero init).Yes, I believe it should work and it can also be applied to other data types.
For class arrays though, if there are fewer items in the initializer list than elements in the array, the default constructor is used for the remaining elements. If no default constructor is defined for the class, the initializer list must be complete — that is, there must be one initializer for each element in the array.
Additionally, keep a note of how the array and vector are default initialized if don't initialize them explicitly.
int main()
{
bool arr1[3]; // true true true [here true is SOME garbage value]
std::vector<bool> vec1(3); // false false false
bool arr2[3] = { true }; // true false false [here true/false are REAL true/false values]
vector<bool> vec2(3, true); // true true true
bool arr3[3] = { false }; // false false false
std::vector<bool> vec3(3, false); // false false false
int arr4[3]; // GV GV GV - garbage value
std::vector<int> vec4(3); // 0 0 0
int arr5[3] = { 1 }; // 1 0 0
std::vector<int> vec5(3, 1); // 1 1 1
int arr6[3] = { 0 }; // 0 0 0
std::vector<int> vec6(3, 0); // 0 0 0
}
true or false if you don't mean true or false. It is realy confusing and misleading. Write xor yor <unspecified>.You can declare the array in C++ in these type of ways.
If you know the array size then you should declare the array for:
integer: int myArray[array_size];
Double: double myArray[array_size];
Char and string : char myStringArray[array_size];
The difference between char and string is as follows
char myCharArray[6]={'a','b','c','d','e','f'};
char myStringArray[6]="abcdef";
If you don't know the size of array then you should leave the array blank like following.
integer: int myArray[array_size];
Double: double myArray[array_size];
falseis the same as0(otherwiseif(false)wouldn't evaluate to false), so what you have will probably work on 99% of compilers. Can't be sure about the other 1% until we cite the standard.int a[10] = { 1, 2, 3 };will seta[3]..a[9]to0, ("initialized implicitly the same as objects that have static storage duration"). Does this hold true for C++?falseis not the same as0, but in{0}the 0 is converted tofalse, and in (for C++){}you don't even have to care about conversions: It's initialized tofalseor0or null-pointer or any other type-sensitive default.