开发者

passing two dimensional C style array to Objective C method

.h file

@interface GameState :NSObject{
int SymbolsPositions[3][5];
}
-(void)SaveCurrentGameState:(int **)Array;
@end


@interface GameViewController : UIViewController 
{
  ...
 int sequence_after_spin[3][5];
  ...
}
-(Void)AMethod;
@end

.m file

@implementation GameState
-(void)SaveCurrentGameState:(int **)Array
{   
开发者_运维知识库    for(int i = 0;i<5;i++)
     for(int j = 0;j<3;j++)
       NSLog(@" %d",Array[j][i]);
}
 @end


@implimentation GameViewController
-(void)AMethod
 {
   [instanceOfGameState SaveCurrentGameState:sequence_after_spin];
  }
@end

the application crashes when ever AMethod is called iget following warning

warning: incompatible pointer types sending 'int [10][5]' to parameter of type 'int **' [-pedantic]


The argument needs to take an array of int[][5], the reason for this is because the compiler needs to know the number of columns to correctly identify where the members are. You could also specify int[10][5] as the parameter type if you always take the same size array.


While there could be other errors, one I see is

for(int i;i<5;i++)
    for(int j;j<3;j++)
        NSLog(@" %d",Array[j][i]);

You are not setting an initial value to i and j. i and j could be any garbage value.

for(int i = 0;i<5;i++)
    for(int j = 0;j<3;j++)
        NSLog(@" %d",Array[j][i]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜