-1

I am making a chess game in Objective-C. In this game I need to use BOOL* instead of BOOL because I need a pointer to a boolean variable. When I try to use BOOL*, it gives me a warning when I try to do this:

BOOL *isWhiteTurn;
isWhiteTurn = YES;

The warning is:

Incompatible integer to pointer conversion assigning to 'BOOL *' (aka 'signed char *') from 'BOOL' (aka 'signed char')

Hima
  • 1,249
  • 1
  • 14
  • 18
Adnan Zahid
  • 573
  • 1
  • 10
  • 38
  • 2
    *Why* do you need a pointer? Some more context would be helpful. – Martin R Apr 24 '15 at 19:07
  • Double check about whether you need a `BOOL*` or a "boxed" BOOL `[NSNumber numberWithBool:YES]` – danh Apr 24 '15 at 19:07
  • http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean – Kreiri Apr 24 '15 at 19:08
  • You should not use pointers unless you know what they are!! – Hot Licks Apr 24 '15 at 19:22
  • If you _need_ to use a pointer, then the computer _needs_ you to use it correctly. What are you trying to accomplish? – jscs Apr 24 '15 at 19:23
  • @JoshCaswell I am using pointers because I am setting the BOOL's value in some other class and I want it to reflect changes everywhere. – Adnan Zahid Apr 24 '15 at 19:36
  • 1
    Down-votes here seem excessive. OP might or might not need a BOOL*, and needs syntax help in any case. It might be a dup, but the cited dup is too general, imo. (or it's a dup of every question about c pointers). – danh Apr 24 '15 at 19:38
  • @AdnanZahid: It would really help if you show the relevant code. Where/how is the value set in some other class? Perhaps you just want to pass the *address* ( `&isWhiteTurn` ) to another function? – Martin R Apr 24 '15 at 19:40
  • @AdnanZahid - It sounds like that other class should have a BOOL property, not a BOOL*. Everyone with a handle to the same instance of that other class will have a pointer to the same structure... they will all see the current values of its properties, including its scalar properties. – danh Apr 24 '15 at 19:41
  • @MartinR thank you so much! Your solution worked. I passed &isWhiteTurn to another function and it worked. Could you write this as an answer so I can accept it? – Adnan Zahid Apr 24 '15 at 19:55
  • @danh - It's down-voted, I suspect, because the question expresses ignorance of a *very* basic concept of C programming that anyone should understand **before** they embark on learning Objective-C. – Hot Licks Apr 24 '15 at 22:16
  • Please [edit] your question to include the information about your problem that's come out in the comments. – jscs Apr 25 '15 at 04:17

2 Answers2

5

A pointer is exactly what is sounds like, it points to some other memory.

Lets take this simple example:

BOOL actualVariable = FALSE;
BOOL *pointerVariable = &actualVariable;

That makes pointerVariable point to actualVariable. Using the derefernece operator (unary *) you can get the value of what a pointer points to:

printf("Value of *pointerVariable = %d\n", *pointerVariable);

That should print

Value of *pointerVariable = 0

More graphically you can look at it this way:

+-----------------+       +----------------+
| pointerVariable | ----> | actualVariable |
+-----------------+       +----------------+

You can also use the dereference operator to change the value of where the pointer points:

*pointerVariable = TRUE;

If you declare a pointer, and don't make it point anywhere, then attempting to dereference the pointer (i.e. get what the pointer points to) will result in undefined behavior.


Now regarding your warning. A pointer variable is actually a simple integer, whose value is the address of where it points. That means you can in theory assign any integer value to it, and the program will think that the value is an address of something valid. Most of the time it is not something valid though.

You get the warning because usually using an integer value to initialize a pointer is the wrong thing to do, you should initialize the pointer with another pointer to the same type.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • "You get the warning because usually using an integer value to initialize a pointer is the wrong thing to do, you should initialize the pointer with another pointer to the same type.", I am not sure what you mean by this. How should I initialize the BOOL pointer to YES? I mean in code. – Adnan Zahid Apr 24 '15 at 19:17
  • @AdnanZahid See the first code lines. You need to make the pointer actually point somewhere, then you can modify the value of where it points. You can't set the value directly using the pointer, you need to set it *indirectly*. – Some programmer dude Apr 24 '15 at 19:19
  • Okay, I did "BOOL temp = YES; BOOL *isWhiteTurn = &temp;". Now when I try to do isWhiteTurn = !isWhiteTurn; I am getting a warning "Incompatible integer to pointer conversion assigning to 'BOOL *' (aka 'signed char *') from 'int'" – Adnan Zahid Apr 24 '15 at 19:26
  • @AdnanZahid I think a more appropriate response actually is: ***Why*** do you want to use pointers for this? – Some programmer dude Apr 24 '15 at 19:26
  • @AdnanZahid Read my answer again, it tells you how to *dereference* the pointer. – Some programmer dude Apr 24 '15 at 19:27
  • Because I am setting the BOOL's value in some other class and I want it to reflect changes everywhere. I'm trying your dereferencing solution, lets see how it goes :) – Adnan Zahid Apr 24 '15 at 19:29
3

As it became apparent in the comments, you have some function taking a BOOL * parameter, for example

void foo(BOOL *boolPtr) {
    *boolPtr = NO;
}

and you need to pass the address of your BOOL variable to that function:

BOOL isWhiteTurn = YES;
foo(&isWhiteTurn);
// Now isWhiteTurn == NO
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382