I am trying to pass a multi dimensional array as a parameter to a function. Here is my complete code :
#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") ;
}
}
I have marked ----->
where I think error is.
Error :
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) ;
I tried looking this post but was unable to detect the error. Also if I replace function prototype with this :
void transpose( int, int, int[][])
then it says function definition incomplete.
So how do I pass a variable sized multi dimensional array ( possibly by avoiding pointers ) ?
Edit :
I had already tried these modifications earlier :
void transpose( int, int , int [] [int] ) //type 1
void transpose( int , int , int[] [col] ) //type 2
None of them work. Here is the error generated
for type 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) ;
For type 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.