简体   繁体   中英

How to pass C-style matrix in Objective-C

I trying to pass matrix to my function:

int movesMatrix[self.map.width][self.map.height];
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        movesMatrix[x][y] = -1;
    }
}

- (void)foo:(int **)movesMatrix
{
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            NSLog(@"%d,%d - %d", y, x, movesMatrix[x][y]);
        }
    }
}

But I getting error:BAD_ACCESS What am I doing wrong?

try to change to following:

- (void)foo:(int *)movesMatrix
...
NSLog(@"%d,%d - %d", y, x, movesMatrix[x*self.map.height + y]);

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