Passing two-dimensional array via pointer

前端 未结 9 1147
失恋的感觉
失恋的感觉 2020-11-28 14:04

How do I pass the m matrix to foo()? if I am not allowed to change the code or the prototype of foo()?

void foo(float **pm)
{
    int i,j;
    for (i = 0; i          


        
9条回答
  •  误落风尘
    2020-11-28 14:31

    • you dont need to do any changes in the main,but you function will work properly if you change the formal prototype of your function to (*pm)[4] or pm[][4] because **pm means pointer to pointer of integer while (*pm)[4] or pm[][4] means pointer to poiner of 4 integers .

      m here is also a pointer to pointer of 4 integers and not pointer to pointer of integers and hence not compatible.

      #include
      void foo(float (*pm)[4])
      {
          int i,j;
          for (i = 0; i < 4; i++)
              for (j = 0; j < 4; j++)
                  printf("%f\n", pm[i][j]);
      
      }
      
      int main ()
      {
          float m[4][4];
          int i,j;
          for (i = 0; i < 4; i++)
              for (j = 0; j < 4; j++)
                      m[i][j] = i+j;
      
          foo(m);
       }
      

提交回复
热议问题