Passing two-dimensional array via pointer

前端 未结 9 1159
失恋的感觉
失恋的感觉 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:15

    You can't. m is not compatible with the argument to foo. You'd have to use a temporary array of pointers.

    int main()
    {
        float m[4][4];
        int i,j;
    
        float *p[4];
    
        p[0] = m[0];
        p[1] = m[1];
        p[2] = m[2];
        p[3] = m[3];
    
        for (i = 0; i < 4; i++)
            for (j = 0; j < 4; j++)
                m[i][j] = i+j;
    
    
        foo(p);
    

提交回复
热议问题