c - Passing multi dimensional arrays as function parameter gives error -


i trying pass multi dimensional array parameter function. here 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 , colummns in matrix :\t") ;     scanf("%d %d", &row, &col) ;     int matrix [row][col] ;     printf("\n") ;     ( temprow ; temprow < row ; temprow++) {         ( 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 -----> 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 ‘transpose’ [enabled default] void transpose(int row, int col, int matrix[row][col]) {   ^ matrixtranspose.c:19:2: note: previous implicit declaration of ‘transpose’ here transpose(row, col, matrix) ; 

i tried looking this post unable detect error. if replace function prototype :

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

then says function definition incomplete.

so how pass variable sized multi dimensional array ( possibly avoiding pointers ) ?


edit :

i had 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 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 ‘transpose’   [enabled default] void transpose( int row, int col, int matrix[row][col]) {   ^ matrixtranspose.c:19:2: note: previous implicit declaration of ‘transpose’ here transpose(row, col, matrix) ; 

for type 2 :

matrixtranspose.c:3:34: error: ‘col’ undeclared here (not in function) void transpose( int , int, int[][col] ) ;                               ^ matrixtranspose.c: in function ‘main’: matrixtranspose.c:19:2: error: type of formal parameter 3 incomplete transpose(row, col, matrix) ; ^   matrixtranspose.c:19: confused earlier errors, bailing out   preprocessed source stored /tmp/ccasypoi.out file, please attach bugreport. 

in short, can't without relying on compiler. pre-c99 doesn't allow multi-dimensional array function arguments there more 1 unknown dimension.

however, not despair! c caters using linear indexing. there's great link covers topic in detail, short answer problem, can fix problem follows:

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

where matrix[(tempcol * col) + temprow] linear index magic you, c can treat multidimentional array of row rows , col columns one-dimensional array row*col elements long (which in fact in memory).

there's question discussing accessing array in c.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -