2009-09-16 41 views
1

爲什麼下面的代碼給我一個分段錯誤?如何修改傳遞給函數的2d數組?

#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聲明都沒有得到傳遞正確的數據類型,但我不知道是什麼,他們應該

我該如何改變它才能正常工作?

+0

您在什麼時候收到seg故障? – 2009-09-16 07:43:17

+0

當它開始將值讀入* data * – 2009-09-16 07:48:40

回答

4

這聲明指針MAXROWS陣列到int陣列。

int *data[MAXROWS][MAXCOLS]; 

然而,在功能定義中,頂層陣列(任意大小的)等價於指針,因爲數組總是衰減到指針陣列構件的類型上傳遞給一個函數。

所以你的函數定義等效於:

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

即指針的MAXCOLS指針數組int

當你的代碼代表,你永遠不會初始化任何int指針數組中,因爲你是路過的int個二維數組的指針的int *二維數組。

你可能想傳遞什麼,是一個指向MAXCOLSint數組:

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

或等價:

void getInput (int data[][MAXCOLS]) 

然後您執行以下操作:

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

    getInput(data); 

    return 0; 
} 

然後,您將第2個數組作爲指針傳遞給第一個數組元素(指向行或數組MAXCOLSint s的指針)。

如果你確保改變一定要更改:

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更多的行和列。

+0

非常感謝您的詳細解釋和示例代碼! – 2009-09-16 07:50:26

0

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

不是,那不起作用 – 2009-09-16 07:47:25

+0

我運行它並且它適用於我。現在有什麼錯誤? 上面的函數中有4個固定了,你做了他們全部嗎? – Amirshk 2009-09-16 07:51:24

0

有幾個不同的問題。

首先,將數組傳遞給函數時,只需要定義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; 
}