2013-10-18 24 views
0

我試圖傳遞矩陣我的功能:如何通過C風格的矩陣在Objective-C

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]); 
     } 
    } 
} 

但我得到錯誤:BAD_ACCESS 我在做什麼錯?

回答

1

嘗試改變到以下幾點:

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

是的,它的工作。但它不是很方便。有沒有能力以另一種方式傳遞矩陣? –

+0

@TimurBernikowich爲了實現你的邏輯,你需要定義函數的輸入參數爲'int movesMatrix [] [####]' - 而不是###你必須**把常量值,但是當前你有變量' self.map.height',如果您將其更改爲任何類型的常量 - 您將能夠使用2d語法 –