Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kubap007

macrumors newbie
Original poster
Jul 21, 2008
9
0
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 :)
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Code:
tmp = (@"%d\t", val);

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...
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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 :)

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.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
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).

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.
 

MiniMacLean

macrumors newbie
Mar 9, 2008
15
0
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
 

MiniMacLean

macrumors newbie
Mar 9, 2008
15
0
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?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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?

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.
 

MiniMacLean

macrumors newbie
Mar 9, 2008
15
0
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
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
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

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
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.
 

MiniMacLean

macrumors newbie
Mar 9, 2008
15
0
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.

thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.