Intializing a Boolean Array [Solved]

Posts 110 of 10 · Page 1 of 1
Intializing a Boolean Array [Solved]
I get this error

Code:
error C2440: 'initializing' : cannot convert from 'bool' to 'bool [3]'

when i do this

Code:
bool flag[3] = true;

How do i make them all true?
[highlight=vb]bool flag[3];
for(int i =0;i<3;i++)
{
flag[i]=true;
}[/highlight]
Quote Originally Posted by Hassan View Post
[highlight=vb]bool flag[3];
for(int i =0;i<3;i++)
{
flag[i]=true;
}[/highlight]
or[highlight=vb]bool flag[3] = {true, true, true};
[/highlight]
Quote Originally Posted by Nathan View Post


or[highlight=vb]bool flag[3] = {true, true, true};
[/highlight]
Yeah, do it 1000 times to initialize 1000 items ?
Quote Originally Posted by Hassan View Post


Yeah, do it 1000 times to initialize 1000 items ?
It's 3, not 1000...
In this case it could be what I posted
Quote Originally Posted by Nathan View Post


It's 3, not 1000...
In this case it could be what I posted
I am sure he does know how to initialize an array of 3 items. Look at this thread: http://www.mpgh.net/forum/31-c-c-pro...ter-array.html

I think he was just using [3] as an example to see how all can be initialized to true.
Quote Originally Posted by Hassan View Post


I am sure he does know how to initialize an array of 3 items. Look at this thread: http://www.mpgh.net/forum/31-c-c-pro...ter-array.html

I think he was just using [3] as an example to see how all can be initialized to true.
Oh okay.
I thought he needed an array of 3.
/Marked Solved.

@Braco22: You don't provide any feedback on whether a solution works for you or not, so if this keeps on going like this, I'll automatically assume that the solution has worked for you.
Hey Hassan, could u tell me if this is right? i want a better understanding of arrays. does an array start with 0 or 1?

if bool [3] = {true, true, true};

would it be listed like this?

a)
bool [0] = true;
bool [1] = true;
bool [2] = true;

or

b)

bool[1] = true;
bool[2] = true;
bool[3] = true;
Quote Originally Posted by Braco22 View Post
Hey Hassan, could u tell me if this is right? i want a better understanding of arrays. does an array start with 0 or 1?

if bool [3] = {true, true, true};

would it be listed like this?

a)
bool [0] = true;
bool [1] = true;
bool [2] = true;

or

b)

bool[1] = true;
bool[2] = true;
bool[3] = true;
An array starts with 0. So, the following would create an array containing 3 elements:

[highlight=vb]bool myArray[3];[/highlight]

As arrays are zero-based, the first element of the array can be accessed by:

[highlight=vb]myArray[0];[/highlight]

and the last element is accessed as:

[highlight=vb]myArray[2];[/highlight]

So, (a) is correct.
Posts 110 of 10 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?