Create a pointer to two-dimensional array

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

    The basic syntax of initializing pointer that points to multidimentional array is

    type (*pointer)[1st dimension size][2nd dimension size][..] = &array_name

    The the basic syntax for calling it is

    (*pointer_name)[1st index][2nd index][...]
    

    Here is a example:

    #include 
    #include 
    #include 
    
    int main() {
       // The multidimentional array...
       char balance[5][100] = {
           "Subham",
           "Messi"
       };
    
       char (*p)[5][100] = &balance; // Pointer initialization...
    
       printf("%s\n",(*p)[0]); // Calling...
       printf("%s\n",(*p)[1]); // Calling...
    
      return 0;
    }
    

    Output is:

    Subham
    Messi
    

    It worked...

提交回复
热议问题