views:

72

answers:

2

Hi

I'm using objective c and trying to output a value from a function. Apparently I am doing something wrong because I'm receiving an incorrect value.

This is my code:

-(float) getAngleBetween {
float num = 0.0;
return num;

}

and I'm calling it as follows:

float *theAngleBetween = [self getAngleBetween];
NSLog(@"Angle.. = %f", theAngleBetween);

Any help please?

+1  A: 
float theAngleBetween = [self getAngleBetween];
//   ^

There should be no *.

Since you are returning a float, the receiver should have type float as well. float* means a pointer to float, which is entirely different from float.

BTW, make sure you declare -(float)getAngleBetween; before you call [self getAngleBetween]. Put it in the @interface. If it is not declared before, the method will be assumed to have the type -(id)getAngleBetween;. On x86 returning a id and a float use different API (objc_msgSend vs objc_msgSend_fpret), which may be the cause of wrong result.

KennyTM
I declared that in my interface and it still tells me that i have incompatible types in initialization.
Lily
@Lily: Have you `#import` the interface file ?
KennyTM
yes because I'm calling another function which i declared in the interface as well.
Lily
sry fixed thanks a lot (declaration in wrong interface :$)
Lily
A: 

You should have:

float theAngleBetween = [self getAngleBetween];

Get rid of the *, that's for objects only, float is a primitive data type.

Jorge Israel Peña
without *, it tells me incompatible types in initialization
Lily