2

Let's say I have a pointer to some object, called myObject, and I need to know, whether it is really pointing to something. How can this code:

// assume MyObjectClass *myObject;
return (BOOL)myObject;

return 112? I know, that I can always write

return (myObject == nil);

and everything will be fine. But until today I have always assumed, that explicit casting of anything to bool will always return true or false (as far as I know, 0 is always considered as false and any other value as true) and that BOOL with it's YES and NO values is just "renamed" bool. So basically, my questions are:

  • Why is it returning 112? :-)
  • Are results of explicit casting defined somewhere in C/Objective-C standard, or is it compiler-specific?
FurloSK
  • 441
  • 2
  • 14
  • Look at the definition for BOOL. Then compare to `int a = 123; BOOL b = (BOOL)123; int c = b;` (It has *nothing* to do with pointers). I think it was C99 that introduced a proper boolean type in C .. but not sure conversions to/from it are defined. (C is not C++). –  Oct 09 '12 at 21:24
  • I don't know about Objective-C, but in C, there is no bool type and anything not zero is considered true. – Alexandre C. Oct 09 '12 at 21:24
  • Try `!!myObject` instead which always has an `int` value of `0` or `1`. – pmg Oct 09 '12 at 21:24

3 Answers3

4

In Objective-C, the BOOL macro is just a typedef for signed char with YES/NO defined, but bool is an actual boolean, which can be true or false.

It returns 112, because it rounds the address of your pointer to signed char type.

Here is some discussion with good answers:
Objective-C : BOOL vs bool
Is there a difference between YES/NO,TRUE/FALSE and true/false in objective-c?

Community
  • 1
  • 1
dreamzor
  • 5,795
  • 4
  • 41
  • 61
  • `bool` is defined as `_bool`, but `_bool` is typedef to `int`... what's the difference, except that `BOOL` is typedef to `signed char`? – FurloSK Oct 09 '12 at 21:35
  • `BOOL` is a macro, `bool` is a C-standard. See this answer: http://stackoverflow.com/a/5472736/1280800 – dreamzor Oct 09 '12 at 21:36
  • Thanks for that link, maybe you could add it to your answer, for everybody else to see it instantly... accepting, btw ;-) – FurloSK Oct 09 '12 at 21:44
1

The definition of "true" in C is any non-zero number. Therefore 112 would be considered true as far as C is concerned. From the C99 standard:

6.3.1.2 Boolean type

When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

Your value is not converted to 1 because you are converting to BOOL not _Bool. The conversion to 0/1 will be technically handled inside the if (though in reality the implementation is more likely to be (myObject != 0) inside any if/while type statement).

Community
  • 1
  • 1
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
1

In C , bool is a stdbool.h macro for boolean type _Bool.

And a conversion of a non-zero integer value to _Bool is guaranteed to yield 1.

That is, the result of 1 == (bool) 42 is 1.

If you are using a BOOL type as an alias for another integer type (like signed char), you can get a different result:

The result of 1 == (BOOL) 42 is 0.

ouah
  • 142,963
  • 15
  • 272
  • 331