Assignment Makes Pointer from Integer Without a Cast

22,584

Solution 1

getUserDefaults takes a BOOL*. You don't get a warning when you pass NO because NO is 0 and 0 is NULL, which is a legal BOOL*. YES is 1 and the same conversion isn't automatically safe.

You should make getUserDefaults take a plain BOOL, instead of a pointer.

Solution 2

BOOL is a primitive type, not a class, so your method declaration should be

-(void)getUserDefaults:(BOOL)refreshDefaults;

instead. The reason you're getting the warning you are is that 0 (which is what NO is defined to be) is a valid pointer value without a cast, equivalent to NULL, but 1 (which is what YES is defined to be) isn't a valid pointer value without a cast.

Solution 3

I have never seen Obj-C code before, but I'm pretty sure it's because the 'refreshDefaults' is a pointer to BOOL whereas it should be a BOOL.

I don't know about the typing rules in Obj-C, but it looks like NO is defined as 0 and the compiler has no problems converting that to a pointer value (a NULL pointer.) That would not be the case with integer '1'. (Also note "if (refreshDefaults)" is valid code that checks the pointer refreshDefaults against the NULL pointer)

Solution 4

Cool thanks! That makes sense, and fixed it! Here is the updated code for reference:

-(void)getUserDefaults:(BOOL)refreshDefaults
{
PostAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

if (refreshDefaults) {
    [appDelegate retrieveDefaults];
}
}

Solution 5

In the first case, NO probably defaults to 0 - the null pointer. This is a valid Bool*. Yes will default to 1, and so when it is cast to a Bool* you are passing the integer 1 in - which is then turned into a Bool*.

Share:
22,584
jarry jafery
Author by

jarry jafery

I program computers, phones, tablets, web servers, robots, anything I can really. I love creating and I love changing people’s lives for the better. Building software is one of the best ways I have found to do both of those things at once.

Updated on October 08, 2020

Comments

  • jarry jafery
    jarry jafery over 3 years

    I have an Obj-C method similar to this:

    -(void)getUserDefaults:(BOOL *)refreshDefaults
    {
        PostAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    
        if (refreshDefaults) {
            [appDelegate retrieveDefaults];
        }
    }
    

    When I call it like this I get no warning:

    [self getUserDefaults:NO];
    

    When I call it like this I get a warning:

    [self getUserDefaults:YES];
    

    warning: passing argument 1 of 'getUserDefaults:' makes pointer from integer without a cast

    NOTE: I always call the method passing NO first, then sometime later I pass YES

    Can anyone fill me in on what the issue is here? Thanks.

  • nobody
    nobody over 15 years
    Pointers to primitives are perfectly legal.
  • suppala
    suppala over 15 years
    Yes, but he's not passing in a pointer to BOOL, he's just passing in a BOOL, which is why he's getting that error.
  • Jason Coco
    Jason Coco over 15 years
    Couple of quick things: 1) you should edit your original question if you want to show working code vs. non-working; this isn't a forum. Second, if somebody helped you, you should give them credit by accepting their answer :)