Do booleans start off true or false?

Apr 10, 2010 at 12:56am
Or do I have to set what they start off as?

If i DO then how do I set all values of a 2 dimensional boolean array to true.
Last edited on Apr 10, 2010 at 1:02am
Apr 10, 2010 at 1:36am
No primitive type has any "starting" value. It's garbage data left over from whatever occupied its memory previously. If you have a lucky compiler extension it might initialize them for you but that's not usually the case. And it's also nonstandard.
Long and short: all primitive types must be initialized or given a value EXPLICITLY by the user prior to use, and all other behavior is undefined.
Apr 10, 2010 at 1:39am
Ok thanks so how would I set bool seats[15][30] to all true?

do i have to go 1 by 1
Apr 10, 2010 at 1:42am
You really want to know what I'd do?
Alright. I'd define a wrapper class with a default constructor that sets them to true. I'd overload =, ==, !=, >> and << for the purpose of operating on this wrapper class. That saves me the time of having to initialize them all myself. Chances are this is not reasonable for one project though.
So what would you do? Try a nested for loop, going through each row and each column. Cycle through the booleans and set them all to true. There's not much else I can think of.
Apr 10, 2010 at 2:12am
Last edited on Apr 10, 2010 at 2:13am
Apr 10, 2010 at 2:23am
Almost. But it's not quite the same.
Apr 10, 2010 at 2:44am
I suppose you could also do something like: memset(seats,1,15*30);

For more info about memset check this out: http://www.cplusplus.com/reference/clibrary/cstring/memset/
Last edited on Apr 10, 2010 at 2:48am
Apr 10, 2010 at 4:13am
prefer std::fill (or std::fill_n) to memset

http://cplusplus.com/reference/algorithm/fill_n/

I especially wouldn't use memset on bools. On an array of bools no less!

 
std::fill_n(&seats[0][0], 15*30, true);
Topic archived. No new replies allowed.