views:

101

answers:

2

Im using core-plot for my graphing component of my iPhone app, and I have been using NSDecimal object a lot.

One of the lines of their code that I have seen is like this:

-(void)plotPoint:(NSDecimal *)plotPoint forPlotAreaViewPoint:(CGPoint)point
{
    NSDecimal x;
    //do some calculations on x
    plotPoint[CPCoordinateX] = x;
}

Where, CPCoordinateX is deinfed as below:

typedef enum _CPCoordinate {
    CPCoordinateX = 0,  ///< X axis
    CPCoordinateY = 1,  ///< Y axis
    CPCoordinateZ = 2   ///< Z axis
} CPCoordinate;

The line:

plotPoint[CPCoordinateX] = x;

is what I dont understand, how can a NSDecimal be assigned to like this?

In my code, Im trying to call this method, like so:

NSDecimal dec = CPDecimalFromInteger(0);
[plotSpace plotPoint:&dec forPlotAreaViewPoint:point];
NSDecimalNumber *newx = [[NSDecimalNumber alloc] initWithDecimal:dec];

NSDecimal x = dec[CPCoordinateX];
//NSLog(@"converted at: %@", newx);

but Im getting compile errors:

error: subscripted value is neither array nor pointer

Can someone please explain this to me?

+4  A: 

It's a C array.

Kurbz
not probably. it is. :)
Dave DeLong
Its not an array, its a pointer on which the subscript operator is used.
Georg Fritzsche
so what is it, a pointer or an array? How can NSDecimal be an array? Isnt it a decimal?
Mark
`plotPoint` is a pointer to a `NSDecimal`. `dec` in your code however is just one decimal value, not a pointer or array
Georg Fritzsche
+5  A: 

plotPoint is a pointer and pointers can be indexed like arrays using the subscript operator:

int array[] = { 1, 2, 3 };
NSLog(@"x=%d, y=%d, z=%d", array[0], array[1], array[2]); 
// prints "x=1, y=2, z=3"
int *pointer = array; // implicit conversion to pointer
NSLog(@"x=%d, y=%d, z=%d", pointer[0], pointer[1], pointer[2]);
// also prints "x=1, y=2, z=3"

You can also use those expressions for assignments:

array[0] = 4;
pointer[1] = 5;

But you can only use the subscript operator on arrays or pointers:

NSDecimal dec = CPDecimalFromInteger(0);
dec[0]; // illegal, dec is a single NSDecimal value, not pointer or array

To actually pass a point -plotPoint:forPlotArrayViewPoint: you need a C-style array or a dynamic array of 2 or 3 NSDecimals (according to what dimensions the method expects), e.g.:

NSDecimal decPoint[] = {
     CPDecimalFromInteger(0),
     CPDecimalFromInteger(0),
     CPDecimalFromInteger(0)
};
[plotSpace plotPoint:decPoint forPlotAreaViewPoint:point];

On that array you can now also use the subscript operator:

NSDecimal x = decPoint[CPCoordinateX];
Georg Fritzsche
Mark
@Mark: In your code `dec` is just a `NSDecimal` value, so `NSDecimal x = dec;` would be sufficient - but `-plotPoint:forPlotArrayViewPoint:` surely expects a point of more than one dimension... See my update.
Georg Fritzsche
Thanks a lot Georg, I understand completely now, however, I think its a bit miss-leading to have the parameter in the method as a `(NSDecimal*)` I get the feeling, for clarity sake, it should be a `NSDecimal[]`. Or perhaps it just one of those things people accept? How would anyone know it was an array if the code wasnt open source?
Mark
@Mark: If it takes a pointer it either expects it to point to the location `>=1` elements or it is an in-out-parameter - what exactly it is would have to be documented. I think a pointer is clearer here because you don't have to pass an array in, it could be a pointer to dynamically allocated memory too (`malloc()`/`calloc()`).
Georg Fritzsche