문제

다음 코드가 나에게 분할 결함을주는 이유는 무엇입니까?

#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);
}

그것은 그게 보인다 scanf 그리고 printf 진술은 올바른 데이터 유형을 통과하지 못하지만 나는 그들이 무엇을 해결할 수 없습니다. ~해야 한다 이다.

올바르게 작동하도록 어떻게 변경하려면?

도움이 되었습니까?

해결책

이것은 배열을 선언합니다 MAXROWS 포인터의 배열 int.

int *data[MAXROWS][MAXCOLS];

그러나 함수 정의에서, 모든 크기의 최상위 배열 (모든 크기)은 배열이 항상 함수로 전달 될 때 배열 멤버의 유형으로 포인터로 부패하기 때문에 포인터와 동일합니다.

따라서 기능 정의는 다음과 같습니다.

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

즉, 배열에 대한 포인터입니다 MAXCOLS 포인터 int.

코드가 서서 int 배열의 포인터는 2D 배열을 통과 할 때 int2D 배열에 대한 포인터로서 int *.

당신이 아마도 통과하고 싶은 것은 배열에 대한 포인터입니다. MAXCOLS int:

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

또는 동등하게 :

void getInput (int data[][MAXCOLS])

그런 다음 다음을 수행합니다.

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

    getInput(data);

    return 0;
}

그런 다음 2D 배열을 첫 번째 요소에 대한 포인터로 전달합니다 (행 또는 배열에 대한 포인터 MAXCOLS int에스).

변경 사항이 변경되면 변경해야합니다.

  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);

포인터를 전달해야합니다 rows 그리고 cols.

입력 함수에 약간의 경계를 추가하여 더 많은 행과 열을 읽지 않도록하십시오. MAXROWS 또는 MAXCOLS.

다른 팁

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]);
        }
    }
}

몇 가지 다른 문제가있었습니다.

첫째, 배열을 함수로 전달할 때는 N-1 차원의 정의 만 있으면됩니다. 예를 들어 3D 배열을 통과하는 경우 기능 SIG에 마지막 2 차원의 크기를 넣고 첫 번째를 비워 두는 것입니다.

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top