【问题标题】:How can I modify a 2d array passed to a function?如何修改传递给函数的二维数组?
【发布时间】:2009-09-16 07:31:58
【问题描述】:

为什么下面的代码给我一个分段错误?

#define MAXROWS 10
#define MAXCOLS 10
void getInput (int *data[MAXROWS][MAXCOLS]) {
  int rows, cols;
  int curRow, curCol;
  printf ("How many rows and cols?");
  scanf ("%d %d", rows, cols);

  for (curRow = 0; curRow < rows; curRow++) {
    for (curCol = 0; curCol < cols; curCol++) {
      scanf ("%d", data[curRow][curCol]);
      printf ("%d\n", *data[curRow][curCol]);
    }
  }
}

void main () {
  int data[MAXROWS][MAXCOLS];

  getInput (data);
}

似乎scanfprintf 语句没有传入正确的数据类型,但我不知道它们应该是什么

如何更改它以使其正常工作?

【问题讨论】:

  • 什么时候出现段错误?
  • 当它开始将值读入 data

标签: c arrays function 2d


【解决方案1】:

这声明了一个 MAXROWS 指向 int 的指针数组。

int *data[MAXROWS][MAXCOLS];

但是,在函数定义中,顶级数组(任意大小)等价于指针,因为数组在传递给函数时总是衰减为指向数组成员类型的指针。

所以你的函数定义相当于:

void getInput (int *(*data)[MAXCOLS])

即指向MAXCOLS 数组的指针,指向int 的指针。

就您的代码而言,您永远不会初始化数组中的任何int 指针,因为您将ints 的二维数组作为指向int * 的二维数组的指针传递。

您可能想要传递的是指向MAXCOLS int 数组的指针:

void getInput (int (*data)[MAXCOLS])

或等效:

void getInput (int data[][MAXCOLS])

然后你执行以下操作:

int main(void)
{
    int data[MAXROWS][MAXCOLS];

    getInput(data);

    return 0;
}

然后,您将二维数组作为指向其第一个元素的指针(指向行或MAXCOLSints 的数组的指针)传递。

如果你确定改变一定要改变:

  scanf ("%d", data[curRow][curCol]);
  printf ("%d\n", *data[curRow][curCol]);

到:

  scanf ("%d", &data[curRow][curCol]);
  printf ("%d\n", data[curRow][curCol]);

另外,请在此处检查您的参数:

scanf ("%d %d", &rows, &cols);

您需要将指针传递给rowscols

确保为您的输入函数添加一些边界检查,这样您就不会尝试读取比MAXROWSMAXCOLS 更多的行和列。

【讨论】:

  • 非常感谢您的详细解释和示例代码!
【解决方案2】:

scanf 接受变量的地址,而不是它的内容:

void getInput (int data[][MAXCOLS]) {
  int rows, cols;
  int curRow, curCol;
  printf ("How many rows and cols?");
  scanf ("%d %d", &rows, &cols);
  //scanf ("%d %d", &rows, &cols);
  for (curRow = 0; curRow < rows; curRow++) {
    for (curCol = 0; curCol < cols; curCol++) {
          scanf ("%d", &data[curRow][curCol]);
          printf ("%d\n", data[curRow][curCol]);
        }
    }
}

【讨论】:

  • 我运行了它,它对我有用。现在是什么错误?上面的函数有4个固定的,你都做了吗?
【解决方案3】:

有几个不同的问题。

首先,将数组传递给函数时,您只需要定义 N-1 维。例如,如果您要传递一个 3D 数组,您可以将最后 2 个维度的大小放在函数 sig 中,并将第一个维度留空。

foo(int threeD[][10][15]);

其次,scanf 获取参数的地址,对于您的数组,它看起来像这样

&data[curRow][curCol]

第三,你应该经常检查你输入的范围以确保它是有效的:

  if (rows > MAXROWS || cols > MAXCOLS) {
    printf("Bad array dimensions\n");
    return;
  }

第四,总是在打开所有警告的情况下编译 - 编译器会警告你分配这些事情:

gcc -Wall pass_array.c -o pass_array

.

#include <stdio.h>

#define MAXROWS 10
#define MAXCOLS 10

void getInput (int data[][MAXCOLS]) {
  int rows, cols;
  int curRow, curCol;
  printf ("How many rows and cols?");
  scanf ("%d %d", &rows, &cols);

  if (rows > MAXROWS || cols > MAXCOLS) {
    printf("Bad array dimensions\n");
    return;
  }

  for (curRow = 0; curRow < rows; curRow++) {
    for (curCol = 0; curCol < cols; curCol++) {
      scanf ("%d", &data[curRow][curCol]);
      printf ("%d\n", data[curRow][curCol]);
    }
  }
}

int main () {
  int data[MAXROWS][MAXCOLS];

  getInput (data);
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多