2015-06-19 131 views
1

我想將多維數組作爲參數傳遞給函數。這是我的完整代碼:傳遞多維數組作爲函數參數給出錯誤

#include <stdio.h> 

----> void transpose(int , int, int[int][int]) ; 

void main() { 
    int row = 0, col = 0 ; 
    int tempRow = 0, tempCol = 0 ; 
    printf("\nEnter no of rows and colummns in the matrix :\t") ; 
    scanf("%d %d", &row, &col) ; 
    int matrix [row][col] ; 
    printf("\n") ; 
    for (tempRow ; tempRow < row ; tempRow++) { 
     for (tempCol ; tempCol < col ; tempCol++) { 
      scanf("%d" , &matrix[ tempRow][ tempCol]) ; 
      printf("\t") ; 
     } 
     printf("\n") ; 
    } 
    -----> transpose(row, col, matrix) ; 
} 

-----> void transpose(int row, int col, int matrix[row][col]) { 
int temp[row][col] ; 
int tempRow = 0 , tempCol = 0 ; 
for(tempRow ; tempRow < row ; tempRow++) { 
    for(tempCol ; tempCol < col ; tempCol++) { 
     temp[tempRow][tempCol] = matrix[tempCol][tempRow] ; 
     printf("%d\t", temp[tempRow][tempCol]) ; 
    } 
    printf("\n") ; 
} 
} 

我已標記----->,我認爲錯誤的。

錯誤:

matrixTranspose.c:3:32: error: expected expression before ‘int’ 
void transpose(int , int, int[int][int]) ; 
          ^
matrixTranspose.c:22:6: warning: conflicting types for ‘transpose’ [enabled by default] 
void transpose(int row, int col, int matrix[row][col]) { 
^
matrixTranspose.c:19:2: note: previous implicit declaration of ‘transpose’ was here 
transpose(row, col, matrix) ; 

我試圖尋找this職位,但無法檢測到的錯誤。另外,如果我用這個替換函數原型:

void transpose(int, int, int[][]) 

然後它說函數定義不完整。

那麼如何傳遞一個可變大小的多維數組(可能通過避免指針)?


編輯:

我已經嘗試了這些修改前面:

void transpose(int, int , int [] [int]) //type 1 

void transpose(int , int , int[] [col]) //type 2 

他們沒有工作。這裏是產生錯誤

類型1:

matrixTranspose.c:3:34: error: expected expression before ‘int’ 
void transpose(int , int, int[][int]) ; 
          ^
matrixTranspose.c:22:6: warning: conflicting types for ‘transpose’ [enabled by default] 
void transpose(int row, int col, int matrix[row][col]) { 
^
matrixTranspose.c:19:2: note: previous implicit declaration of ‘transpose’ was here 
transpose(row, col, matrix) ; 

對於2型:

matrixTranspose.c:3:34: error: ‘col’ undeclared here (not in a function) 
void transpose(int , int, int[][col]) ; 
          ^
matrixTranspose.c: In function ‘main’: 
matrixTranspose.c:19:2: error: type of formal parameter 3 is incomplete 
transpose(row, col, matrix) ; 
^ 
    matrixTranspose.c:19: confused by earlier errors, bailing out 
    Preprocessed source stored into /tmp/ccasypOi.out file, please attach this to your bugreport. 
+0

'void main()'應該是'int main(void)'。 – mch

+0

@mch'void main()'在C中是完全可以接受的(儘管一些編譯器會扼殺它)。 –

+0

@ShotgunNinja恐怕不是。 §5.1.2.2.1,'C11'。 –

回答

2

您需要更改

void transpose(int , int, int[int][int]) ; 

void transpose(int row, int col, int[ ][col]) ; 

,並使用-std=c99

+0

@ Don'tYouWorryChild對,我剛剛刪除了兩個。 –

+0

不工作。我也嘗試了void transpose(int,int,int [] [int])。這也不起作用。看看我的編輯帖子(從現在開始) –

+0

@Plutoniumsmuggler現在怎麼樣? –

1

聲明函數只是像

void transpose(int row, int col, int[][col]) ; 

前提是你的編譯器支持可變長度數組編譯。

接近於您嘗試聲明函數另一種方法是

void transpose(int , int , int [][*]); 

這裏是一個示範項目

#include <stdio.h> 

void f(int , int , int [][*]); 

int main(void) 
{ 
    int row = 10; 
    int col = 10; 

    int a[row][col]; 

    f(row, col, a); 

    for (int i = 0; i < row; i++) 
    { 
     for (int j = 0; j < col; j++) printf("%2d ", a[i][j]); 
     printf("\n"); 
    } 

    return 0; 
} 

void f(int row, int col, int a[][col]) 
{ 
    for (int i = 0; i < row; i++) 
    { 
     for (int j = 0; j < col; j++) a[i][j] = i * col + j; 
    } 
} 

程序輸出爲:)

0 1 2 3 4 5 6 7 8 9 
10 11 12 13 14 15 16 17 18 19 
20 21 22 23 24 25 26 27 28 29 
30 31 32 33 34 35 36 37 38 39 
40 41 42 43 44 45 46 47 48 49 
50 51 52 53 54 55 56 57 58 59 
60 61 62 63 64 65 66 67 68 69 
70 71 72 73 74 75 76 77 78 79 
80 81 82 83 84 85 86 87 88 89 
90 91 92 93 94 95 96 97 98 99 
+0

@Plutonium走私者另請參閱我更新的職位。 –

+0

是的。這工作,雖然現在我的程序邏輯似乎有缺陷。但程序編譯並運行。謝謝 –

+1

@Plutonium走私者我已經包含了一個示範程序。:) –

3

總之,你不能沒有依靠某個編譯器。 Pre-C99不允許具有多個未知維度的多維數組函數參數。

但是,不要絕望! C使用線性索引來迎合這一點。There's a great link which covers the topic in detail,但作爲一個簡短的回答你的問題,你可以按如下解決您的問題:

void transpose(int row, int col, int matrix[]) 
{ 
    int temp[row][col] ; 
    int tempRow = 0 , tempCol = 0 ; 

    for(tempRow ; tempRow < row ; tempRow++) 
    { 
     for(tempCol ; tempCol < col ; tempCol++) 
     { 
     temp[tempRow][tempCol] = matrix[(tempCol * col) + tempRow] ; 
     printf("%d\t", temp[tempRow][tempCol]) ; 
    } 
    printf("\n") ; 
    } 
} 

哪裏matrix[(tempCol * col) + tempRow]是不適合你的魔法線性指標,如C也可以把你的row行多維數組和col列作爲長度爲row*col的一維數組(實際上它實際上在內存中)。

There's also another SO question discussing accessing an array like this in C

+0

嗯,你的答案是唯一正確的,直到C99。但符合C99標準的編譯器現在支持可變長度數組。所以它現在只適用於K&R C和ANSI C :-) –

+0

@SergeBallesta非常真實,雖然我總是喜歡知道如何在C中做某些事情而不依賴C99作爲編譯器選項(我在嵌入式平臺上工作這種奢侈品並不總是可用的);-) –

相關問題