Assignment makes pointer from integer without a cast

Discussion in 'Mac Programming' started by kubap007, Jul 29, 2008.

  1. kubap007 macrumors newbie

    Joined:
    Jul 21, 2008
    #1
    Hello!

    I know it is common problem but I read many posts and I still don't know what I'm doing wrong.
    That is my code:

    Code:
    ...
    short val;
    NSString *tmp;
    
    val = [[value valueForKey:@"stock"] shortValue];
    // everything works alright I checked using NSLog(@"%d", val). I have e.g. 10.
    tmp = (@"%d\t", val);
    
    And I have warning "Assignment makes pointer from integer without a cast", but if I start program and using this function it turns off.
    I will be grateful if someone will help me :)
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Your problem is this line. What, exactly, do you think it's doing? It's certainly not creating a new NSString using the format provided.

    You want to use the stringWithFormat: NSString method like this:

    Code:
    tmp = [NSString stringWithFormat:@"%d\t", val];
    
    Note the string will be autoreleased: you may want to retain it...
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    Just about how you should approach something like this (because at some point you'll be on your own):

    The warning means exactly what it says: You are assigning a value. The value that you are assigning is an integer. The variable that you are assigning to is a pointer. That deserves a warning (actually, gcc is a bit stupid. This shouldn't be a warning, it should be a hard error).

    Obviously that is not what you intended to do. But fact is that you are assigning an integer. The thing you are assigning is

    Code:
    (@"%d\t", val)
    So the compiler tells you it is an integer. Now what do you think this expression would mean? You have an Objective C string @"%d\t", followed by a comma, followed by an integer, and brackets around everything. We can ignore the brackets; what is inside the brackets is a so-called "comma-expression": Two expressions separated by a comma. In a comma-expression, the compiler evaluates the first expression, throws away the result, and then evaluates the the second expression, and that is the result. So you are really assigning
    Code:
    tmp = val
    .

    Where a comma expression is useful sometimes is something like

    Code:
    z = (x = 3*y + 1, x*x);
    which sets x to 3*y + 1, then stores x*x into z (whether that is very readable code is debatable, but that is what the comma expression does). In your case it is clearly not what you wanted. You have to figure out what you actually wanted to do, and write the code that does it.

    It takes a bit of practice to read warnings, but it is essential that you understand them. It is a good idea to have your code completely free of warnings so if any warning turns up after a change you notice it and fix the problem. What is very tempting and very very wrong is trying to "fix" the warning instead of the problem. In your case, you could write

    Code:
    tmp = (NSString *) (@"%d\t", val);
    and the warning would be gone. However, the problem would still be there, except that you told the compiler not to warn you about it. (The compiler would take the integer "val", and pretend that it is a pointer to an NSString object instead of an integer. Results will be very, very ugly).

    Just one thing that I am wondering about: Why do you artificially limit your code to values of "stock" from about -32000 to +32000? This doesn't serve any purpose at all.
     
  4. kubap007 thread starter macrumors newbie

    Joined:
    Jul 21, 2008
    #4
    Thanks now it works, and I learn something too :)
     
  5. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #5
    I'm not sure about that. The C99 specification specifically states (6.5.16.1) that it is not valid C, but I think Objective-C predates this standard (and I'm not sure it is defined to be an error there). Microsoft Visual Studio 2005 (I don't have a more recent version handy here) thinks that it's an error when compiling it as C++ code (error C2440: '=' : cannot convert from 'int' to 'int *') but only issues a warning when compiling it as C (warning C4047: '=' : 'int *' differs in levels of indirection from 'int').

    Just nitpicking.
     
  6. MiniMacLean macrumors newbie

    Joined:
    Mar 9, 2008
    #6
    sorry if i have bumped the thread a bit but, i got an integer declared in viewDidLoad
    Code:
    int *TabNumber;
    and then when a section of a UISegmentedControl is pressed i want that integer to change to 1 so i did this
    Code:
    TabNumber = 1;
    however it comes up with the assignment makes pointer from integer without a cast error,

    how do i fix it, i have read the post above but i dont see how it relates to this
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    The basic problem is that you don't have an integer declared. You have a pointer to an integer declared.
     
  8. MiniMacLean macrumors newbie

    Joined:
    Mar 9, 2008
    #8
    d'ya mean if i delcare the int in the .h like
    Code:
    int			*TabNumber;
    because i have also done that, if not what do you mean?
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    OK, this is going to sound very harsh, but you need to go and learn basic C as you have a fundamental problem with your understanding of what a pointer is, where you use it and what it means.

    Code:
    type *variableName;
    
    declares a pointer to type (specifically a space in memory which is expected to contain data with that type) with the specified name. It does not declare a variable of type. So you have no declared an int, rather a pointer to an int. These two things are critically and fundamentally different.
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    int * is a fundamentally different type than int.

    If you declared TabNumber:
    Code:
    int TabNumber;
    it would be an int, and you could do:
    Code:
    TabNumber = 1;
    You declared it as an int *:
    Code:
    int *TabNumber;
    So to store something in it, you need to get some memory for it:
    Code:
    TabNumber = (int *)malloc(sizeof(int));
    *TabNumber = 1;
    
    If you don't yet know the difference between a pointer and a concrete variable (no offense intended, i'm just truly not sure if this is something you've learned yet), you should find out before proceeding.

    -Lee

    Edit: robbieduncan beat me to the punch, and expressed about the same thing.
     
  11. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    But you did it in much more detail :)
     
  12. MiniMacLean macrumors newbie

    Joined:
    Mar 9, 2008
    #12
    i know you will tell me to read up more about basic C language but
    i want to be able to use this integer in more than one method.
    But when doing this it says warning:local declaration of TabNumber hides instance variable
    sorry if i am being really stupid but could you please explain this warning
     
  13. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #13
    Then you will need to pass the integer between methods. In C you would do something like this:

    Code:
    int main(void)
    {
        int myInt = 7, returnedInt = 0;
    
        returnedInt = randomFunc(myInt);
    
        printf("%i", returnedInt);
    
        return 0;
    }
    
    int randomFunc(int myPassedInt)
    {
        //completely pointless function
        return myPassedInt * 2;
    }
    Similar concept in Objective-C slightly different syntax I assume.
     
  14. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #14
    We got a bit off track to try to teach you something rather than just giving you an answer. To declare TabNumber as an int, just take the * out of your declaration.

    -Lee
     
  15. MiniMacLean macrumors newbie

    Joined:
    Mar 9, 2008
    #15
    thanks
     
  16. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #16
    I strongly recommend Practical C Programming published by O'Reilly and Associates -- the so-called "cow book".

    If you don't want to buy a copy, find it at a library if you can. I was able to teach myself C programming using nothing but this book as a guide, and object-oriented programming (C++) came quickly afterward. It is THE best C book I have ever read.
     
  17. MiniMacLean macrumors newbie

    Joined:
    Mar 9, 2008
    #17
    thanks!
     

Share This Page