如何取消引用此2D动态数组?

时间:2021-08-05 21:36:28

I am having issues with dereferencing the 2D dynamic array in the if statement's condition on line 5.

我在第5行的if语句条件中解除引用2D动态数组的问题。

typedef char* CharArrayPtr;

void reserveSeat(CharArrayPtr *m, char row, char seatLetter){
    for(int j = 1; j < 5; j++){
        if(m[row - 1][j] == seatLetter)
            m[row - 1][j] = 'X';
    }
}

I've tried putting the * in front, but I get this error message:

我已经尝试将*放在前面,但是我收到此错误消息:

Indirection requires pointer operand ('int' invalid)

间接需要指针操作数('int'无效)

Any help is appreciated, thanks in advance.

任何帮助表示赞赏,提前谢谢。

1 个解决方案

#1


-2  

If you mean to dereference the 2D array by operator *, please try following.

如果您的意思是通过运算符*取消引用2D数组,请尝试以下操作。

typedef char* CharArrayPtr;

void reserveSeat(CharArrayPtr *m, char row, char seatLetter){
    for(int j = 1; j < 5; j++){
        //if(m[row - 1][j] == seatLetter)
        if( *( (char*)m + (row - 1)*5 + j ) == seatLetter)
            m[row - 1][j] = 'X';
    }   
}

#1


-2  

If you mean to dereference the 2D array by operator *, please try following.

如果您的意思是通过运算符*取消引用2D数组,请尝试以下操作。

typedef char* CharArrayPtr;

void reserveSeat(CharArrayPtr *m, char row, char seatLetter){
    for(int j = 1; j < 5; j++){
        //if(m[row - 1][j] == seatLetter)
        if( *( (char*)m + (row - 1)*5 + j ) == seatLetter)
            m[row - 1][j] = 'X';
    }   
}