简体   繁体   中英

How to send C-style array to objective-c method

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!

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

-(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 * .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM