char * to bool

T

Travis

I'm curious what the most reliable way to convert a char * (or a char
for that matter) to a bool is.

It's safe to assume that char * will be either '0' and or '1' and I
want the bool set accordingly.

I'm seeing stuff (from others) like this :

bool myBool = (char)atoi(myCharPtr);
 
T

Travis

I'm curious what the most reliable way to convert a char * (or a char
for that matter) to a bool is.

It's safe to assume that char * will be either '0' and or '1' and I
want the bool set accordingly.

I'm seeing stuff (from others) like this :

bool myBool = (char)atoi(myCharPtr);

Also to be noted, I also see

bool myBool = !( (char)atoi(myCharPtr) );

I know ! is a bitwise NOT so how does that work against a char? It's
flipping all the bits and then the bool is determined how? Based on
the value of the lowest bit?
 
D

David Harmon

On Thu, 12 Jul 2007 23:59:06 -0000 in comp.lang.c++, Travis
I'm curious what the most reliable way to convert a char * (or a char
for that matter) to a bool is.

All methods that work are equally reliable, no?
It's safe to assume that char * will be either '0' and or '1' and I
want the bool set accordingly.

bool result = the_char != '0';
bool result = *char_ptr != '0';
I'm seeing stuff (from others) like this :

bool myBool = (char)atoi(myCharPtr);

Insane.
 
R

Robert Bauck Hamar

Depends on what you want.

You mean that the char pointed to is '0' or '1'? Like

char a = '0';
char *p = &a;

bool b = *p != '0';

b will be true if a is not '0', and false if a == '0'.

atoi tries to convert a sequence of numeric characters in a C style string
to an int. The cast transforms this result to a char. bool would be true if
the value returned fits into a bool and is nonzero or there is no numbers
at the beginning of the string (except whitespace). It would be false if
the value is zero, and you would have a bug if it doesn't fit into a char.
Also to be noted, I also see

bool myBool = !( (char)atoi(myCharPtr) );

I know ! is a bitwise NOT so how does that work against a char?

No, it's a boolean not. !true == false, and !false == true. A char is
nothing more than an integer in memory, so a value different from 0
converts to true, and a 0 converts to false.
It's
flipping all the bits and then the bool is determined how? Based on
the value of the lowest bit?

No. ! converts its operand to a bool by the rule I sketched above, and then
flips the truth value.
 
Z

Zachary Turner

bool myBool = !( (char)atoi(myCharPtr) );

I know ! is a bitwise NOT so how does that work against a char? It's
flipping all the bits and then the bool is determined how? Based on
the value of the lowest bit?

You're thinking of ~ (tilde). ! is a logical not. !0 = true, and !
anything else = false
 
T

Travis

You're thinking of ~ (tilde). ! is a logical not. !0 = true, and !
anything else = false

Ah you're right. Thanks.

Doing something like bool myBool = myChar != '0' makes sense.

Thanks everyone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top