我想將多維數組作爲參數傳遞給函數。這是我的完整代碼:傳遞多維數組作爲函數參數給出錯誤
#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.
'void main()'應該是'int main(void)'。 – mch
@mch'void main()'在C中是完全可以接受的(儘管一些編譯器會扼殺它)。 –
@ShotgunNinja恐怕不是。 §5.1.2.2.1,'C11'。 –