Create a pointer to two-dimensional array

后端 未结 10 1309
温柔的废话
温柔的废话 2020-11-22 07:16

I need a pointer to a static 2-dimensional array. How is this done?

static uint8_t l_matrix[10][20];

void test(){
   uint8_t **matrix_ptr = l_matrix; //wron         


        
10条回答
  •  孤独总比滥情好
    2020-11-22 07:45

    You can always avoid fiddling around with the compiler by declaring the array as linear and doing the (row,col) to array index calculation by yourself.

    static uint8_t l_matrix[200];
    
    void test(int row, int col, uint8_t val)
    
    {
    
       uint8_t* matrix_ptr = l_matrix;
       matrix_ptr [col+y*row] = val; // to assign a value
    
    }
    

    this is what the compiler would have done anyway.

提交回复
热议问题