Create a pointer to two-dimensional array

后端 未结 10 1313
温柔的废话
温柔的废话 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:27

    In C99 (supported by clang and gcc) there's an obscure syntax for passing multi-dimensional arrays to functions by reference:

    int l_matrix[10][20];
    
    void test(int matrix_ptr[static 10][20]) {
    }
    
    int main(void) {
        test(l_matrix);
    }
    

    Unlike a plain pointer, this hints about array size, theoretically allowing compiler to warn about passing too-small array and spot obvious out of bounds access.

    Sadly, it doesn't fix sizeof() and compilers don't seem to use that information yet, so it remains a curiosity.

提交回复
热议问题