tags:

views:

320

answers:

7

Hi everyone,

So I'm learning C++ at the moment and I'm trying to work things out with pointers. Could anybody explain the following scenario:

bool testFalse = false;  //testFalse = false
bool *thisIsFalse = &testFalse; //value of address of thisIsFalse shows that the address contains false
bool shouldBeFalse = &thisIsFalse; //shouldBeFalse = true is what I get here

shouldBeFalse (as the name implies) should be false, but it certainly doesn't turn out that way. Could someone explain why it ends up as true and how to make it so it takes on the proper value? Thanks!

+5  A: 

You're using & where you should use *:

bool shouldBeFalse = *thisIsFalse
Alex Black
D'oh! Thanks :)
Alex Black
+1  A: 

thisIsFalse is a variable, and &thisIsFalse is the address of that variable. Since the address is not 0, it is cast to bool as true. I'm not sure what you were going for with that line.. Perhaps you meant this?

bool shouldBeFalse = *thisIsFalse;

Nick Lewis
A: 

In order to talk about boolean values, you should dereference your pointer (*), not take its address (&).

bool shouldBeFalse = *thisIsFalse;
Daniel Daranas
+2  A: 

It isn't entirely clear what you're trying to do, but my guess would be that you actually wanted to dereference the pointer (i.e. retrieve the pointed value). For this you need operator *, not &:

 bool shouldBeFalse = *thisIsFalse;

What your code did was take the address of variable thisIsFalse instead, producing a pointer to pointer. Now in C++, pointers are considered "false" if they are NULL, and "true" otherwise; and the compiler will implicitly convert pointer to bool under those rules if you pass a pointer where a bool is expected. Since you had a non-NULL pointer there, it got converted to true.

Pavel Minaev
+1  A: 

What you did here is assign the address of the thisIsFalse variable to shouldBeFalse. What you meant to do is dereference thisIsFalse. Specifically, since the thisIsFalse variable is stored at a non-null (non-zero) memory location, then it evaluates to true.

bool shouldBeFalse = *thisIsFalse; // now it really is false
280Z28
A: 

testFalse is a boolean assigned a value of false.

thisIsFalse is a pointer to a boolean and is assigned a value equal to the address of testFalse (I assume you meant to type &testFalse rather than &test). This means that when using *thisIsFalse, it will be pointing to the value stored by testFalse, in this case, false. If you change the value of testFalse to true, then *thisIsFalse will be true.

shouldBeFalse is a boolean, but you've assigned it a value equal to the address of thisIsFalse. This address is a nonzero value, and in C++ any nonzero value is considered to be true, so shouldBeFalse is now true.

Instead, you should change the final line to assign to shouldBeFalse the dereferenced value of thisIsFalse, not the address of.

bool shouldBeFalse = *thisIsFalse;
Curtis Tasker
A: 

It's helpful to translate the symols into words so you understand what you're writing exactly.

when to the right of the equal sign, mentaly substitute '*' with "the value pointed to by" and '&' with "the address of".

if you had done this you would have read your code verbaly as follows:

#testFalse is False;
#thisIsFalse is **the address of** testFalse;
#shouldBeFalse is **the address of** thisIsFalse;

but what you wanted was:

#shouldBeFalse is **the value pointed to by** thisIsFalse;

from there it's easy to remember the roles of '*' and '&' so you could translate it back into:

bool shouldBeFalse = *thisIsFalse;
Victor