Skip to main content
gamedev.net
🔒 Locked

Simple array question...

Started by snyp Apr 27, 2004 at 6:59 PM 2 replies 200+ views
Original Post
snyp
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;
}
Actual Linux penguins were harmed in the making of this message.
SantaClaws
SantaClaws
Only initializes the first array element for me in GCC.

[edited by - SantaClaws on April 27, 2004 8:18:41 PM]
mattnewport
mattnewport
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 }

BriTeg
BriTeg
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]
Brianmiserere nostri <font color=#BC

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.