views:

50

answers:

2

I'm working with someone else's code, and there is a float with some unusual qualities.

If I output the float using:

NSLog(@"theFloat: %f", record.theFloat);

I get:

theFloat: 0.000000

However, if I use:

NSLog(@"(int)theFloat = %i", (int) record.theFloat);

I get:

(int)theFloat: 71411232

How do I discover the real format and value of theFloat? I know that it should contain a large number.

Incidentally, the Record class which contains the float propertizes it in such a way:

@property (assign) float* theFloat;

There is also floatLength:

@property (assign) int floatLength;

And has this method, which seems to indicate that the float is of variable length (?):

- (void) copyFloat:(float*)theF ofLength:(int)len
{
    float *floatcopy = malloc(len*sizeof(float));
    memcpy(floatcopy, theF, len*sizeof(float));
    self.theFloat = floatcopy;
}
A: 

The address in memory where the float is stored is given by : (int)theFloat: 71411232 You probably want to use something like :

NSLog(@"theFloat = %f", (*record.theFloat));

Which will dereference the pointer and give you the actual data.

Cobusve
This is completely wrong. Casting to int will truncate a float into an integer. In C++ terms, it's a `static_cast`, not a `reinterpret_cast`. And this only prints the first value of `len`.
Matthew Flaschen
That was a bit harsh I would say. I deleted the aside comment at the end, but the poster said he had a problem with "a float" and I simply took his code that printed the single value, the address of the array, and added the dereference as it is clear he wants to print the value and not the address.I would have said the comment at the end is wrong, bulk of the answer looks just fine, of course if he wants all of the items int the array do it like so ...
Cobusve
A: 

Your field, theFloat, is not a primitive type but a pointer. float* means it is a pointer to a float. You need to dereference the field in order to get it's value.

Use *theFloat to get the actual value.

Also, I suggest you review your format specifiers.

http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265

logancautrell