Original Post
🔒 Locked
Simple array question...
Advertisement
Only initializes the first array element for me in GCC.
[edited by - SantaClaws on April 27, 2004 8:18:41 PM]
[edited by - SantaClaws on April 27, 2004 8:18:41 PM]
If you specify fewer elements in the initializer list than are in the array then the remaining elements are value-initialized, which means they are initialized with their default constructor for classes and with 0 for built in types. For bool zero-initializing is initializing to false so in your example all elements would be initialized to false. It doesn''t quite work as you probably were expecting though:
bool boolArray1[100] = { false }; // All elements initialized to false
bool boolArray2[100] = {}; // All elements initialized to false
bool boolArray3[100] = { true }; // First element initialized to true, remaining elements initialized to false
int intArray[6] = { 0, 1, 2 }; // intArray == { 0, 1, 2, 0, 0, 0 }
quote:
Original post by snyp
bool b[100]={false};
will this turn every array element to false? never have seen this... I'm used to doing
for(i=0;i<=100;i++){
b[i] = false;
}
if you declare b as an array of 100, you get 100 bools: indexes 0 to 99. Your for loop is overrunning your array, for you are initializing indexes 0 to 100, which is 101 bools.
[edited by - BriTeg on April 27, 2004 11:01:30 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement