Lilz
Lilz

Reputation: 4081

Objective c - float returning wrong value

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?

Upvotes: 0

Views: 1843

Answers (2)

kennytm
kennytm

Reputation: 523154

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.

Upvotes: 1

Jorge Israel Peña
Jorge Israel Peña

Reputation: 38578

You should have:

float theAngleBetween = [self getAngleBetween];

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

Upvotes: 0

Related Questions