1

I am trying to send c-style array of integers to objective-c method, but in method I recieve only first element of array. This is an example:

int a[3];
a[0] = 111;   a[1] = 222;   a[2] = 333;
[self getMatrix:a];

then

-(void)getMatrix:(int[3])matrix
{
    return;      -- breakpoint here
}

So, when debugging stops at breakpoint, i have matrix:

matrix  int *   0xbfffddd4
*matrix int 111

Where are other elements? So, 2d-arrays become to 1d array, 3d-array become 2d-array etc. What I am doing wrong?

Thanks!

P.S. NSArray is working fine ofc, but I can't imagine how to work with multidimensional (3d, 4d) NSArrays w/o writing tonns of code.

1
  • You can put NSArrays as elements of NSArrays. And for solving your problem you should have a look into call by reference.
    – dasdom
    Aug 22, 2011 at 15:45

2 Answers 2

2
-(void)getMatrix:(int[3])matrix
{
    int a = matrix[0];
    int b = matrix[1];
    int c = matrix[2];
}

You will see that the values are correct. There is nothing wrong. The debugger is only showing *matrix which is the first element. Here matrix itself is a pointer to integer or int *.

1

You need to send pointer to first item and size of array through.

See how

- (void)setLineDash:(const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase

in NSBezierPath does this.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSBezierPath_Class/Reference/Reference.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.