Passing two-dimensional array via pointer

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

    If you can't change foo(), you will need to change m. Declare it as float **m, and allocate the memory appropriately. Then call foo(). Something like:

    float **m = malloc(4 * sizeof(float *));
    int i, j;
    for (i = 0; i < 4; i++)
    {
        m[i] = malloc(4 * sizeof(float));
        for (j = 0; j < 4; j++)
        {
            m[i][j] = i + j;
        }
    }
    

    Don't forget to free() afterwards!

提交回复
热议问题