Pass matrix as argument

做~自己de王妃 提交于 2019-12-05 09:33:25

The only safe way that I know of to do this is to include the matrix dimensions in the parameters, or make some kind of matrix struct

Option A) dimensions as parameters

void f(int **m, int w, int h )
{
    int i,j;
    for(i=0;i<w;i++)
    {
      for(j=0;j<h;j++)
         printf("%5d", m[i][j]);
    }
    return;
}

Option B) Use a struct

typedef struct Matrix
{
    int w, h;
    int** m;
} Matrix;

void f ( Matrix *m )
{
    for ( int i = 0; i < m->w; ++i )
    {
        for ( int j = 0; j < m->h; ++j )
        {
            printf(%5d", m->m[i][j]);
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!