C:通过函数调用完成时指针算术不起作用?

C: pointer arithmetic not working when done through a function call?

我有一个动态分配的“矩阵”,分配方式如下:

int **m = (int **) malloc (sizeof (int *) * rows);
for (i = 0; i < rows; i++)
    m[i] = (int *) malloc (sizeof (int) * cols);

我想“截断”这个矩阵,我的意思是假设我有这样的东西:

1 2 3
4 5 6
7 8 9

,将其截断 1 行会给我:

4 5 6
7 8 9

我可以很容易地做到这一点

m++;
rows--;

这会产生所需的结果,但是如果我将上述两个语句移动到这样的函数中:

void truncate (int **m, size_t *rows)
{
   m = m + 1;
   *rows = *rows - 1;
}

它没有按预期工作。下面调用

truncate (m, &rows);

产生

1 2 3
4 5 6

我做错了什么?我想这样做的原因是为了概括这一点并允许截断任意数量的行,因此我可以添加第三个参数并按该数字递增 m 并递减 *rows

truncate 内部,您必须增加原始 m 的值。这意味着通过引用而不是值来传递 m 。因此,您必须使用 int ***m:

void truncate (int ***m, size_t *rows)
{
   (*m)++;
   (*rows)--;
}