Thread: defining and calling functions with multidimentional arrays as arguments

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question defining and calling functions with multidimentional arrays as arguments

    Hello this is my first post, so... please help me.
    I want to create a function that prints the content of a matrix(2d array).
    I know that in order to set a matrix as a argumentin a function I have to do it like this
    (example)
    Code:
    void printMatrix(matrix[][N], size){
    int i,j;
    for(i=0;i<size;i++)
    for(j=0;j<N;j++)
    printf("matrix[%d][%d]",i,j);
    }
    or something like that... but in this case I am supposed to know the value of N.
    and that is my problem. Because i don't know the value of N
    I want to create a function that can work on any size of matrix

    a friend told me that :

    Code:
    void printMatrix( *matrix[], size){
    int i,j;
    for(i=0;i<size;i++)
    for(j=0;j<size;j++)
    printf("matrix[%d][%d]",i,j);
    }
    would work fine.... and actually when compiling there is no problem in the declaration, but the problem is in the calling line.

    How should i call this function?
    (i was calling it
    printMatrix(someMatrix,6)
    but it doenst work fine)
    help.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    Greece
    Posts
    10
    If i understood correctly you need dynamic memory allocation not static.The problem is that you know the dimensions of the array only at run time?
    Last edited by Levia8an; 11-23-2006 at 12:54 PM.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    No, I don't need them at run time, but I want to use this function with different values of size in my program, ...., not only once.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    Greece
    Posts
    10
    Ok again, i am not quite sure i understood, but what if you tried this :
    Code:
    //prototype
    void printMatrix( int ** matrix_name , int number_of_rows , int number of columns ) ; 
    
    //call it like this somewhere in a function
    
     printMatrix( someMatrix , 5 , 5 )  ; // in that case someMatrix is [5][5]
    Hmmmm dont spend time tryin this . I did ; it 's mistaken
    Last edited by Levia8an; 11-23-2006 at 01:15 PM.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question

    Code:
    void printMatrix( int **matrix, int size)
    {
    int i,j;
    for(i=0;i<size;i++)
       for(j=0;j<size;j++)
          printf("matrix[%d][%d]",i,j);
    }
    I wrote that .... but :
    i60-41-158-186:~/Desktop/tuesday4_2 nacho$ gcc 1122.c
    1122.c: In function 'printflow':
    1122.c:106: warning: passing argument 1 of 'printMatrix' from incompatible pointer type
    i60-41-158-186:~/Desktop/tuesday4_2 nacho$

  6. #6
    Registered User
    Join Date
    Nov 2006
    Location
    Greece
    Posts
    10
    Yeap it is wrong .Sorry .I guess i should have tried at first place .Before posting

  7. #7
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question Help!!, at first I though it was ok, but not....

    this is my code:
    Code:
    #include<stdio.h>
    int main(){
    int matrix1[5][5]={0,0,0,0,0,0,4,0,0,0,2,0,0,0,3,0,0,0,9,0,4,5,0,0,0};
    int matrix2[4][4]={0,5,0,0,6,0,0,0,0,0,0,0,0,0,3,0};
    int matrix3[2][2]={0,4,0,0};
    printf("calling printMatrix");
    printMatrix(matrix1,5);
    printMatrix(matrix2,4);
    printMatrix(matrix3,2);
    
    return 0;
    
    }
    void printMatrix( int **matrix, int size)
    {
    	int i,j;
    	for(i=0;i<size;i++){
    		for(j=0;j<size;j++)
    			printf("%3d",matrix[i][j]);
    		printf("\n");
    	}
    }
    and when compiling
    Code:
    i60-41-158-186:~/Desktop/tuesday4_2 nacho$ gcc codeTesting.c
    codeTesting.c:15: warning: conflicting types for 'printMatrix'
    codeTesting.c:7: warning: previous implicit declaration of 'printMatrix' was here
    and when executing...

    Code:
    i60-41-158-186:~/Desktop/tuesday4_2 nacho$ ./a.out
    Bus error
    i60-41-158-186:~/Desktop/tuesday4_2 nacho$
    what to do??
    Last edited by nacho4d; 11-23-2006 at 01:41 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can't just declare a parameter type with ** and expect to pass any old 2D array to the function.

    http://c-faq.com/aryptr/ary2dfunc2.html

    If you have a function
    void f ( int a[ ][10] );

    Then you can have
    int a[5][10], b[10][10], c[1000][10];
    and calls
    f(a);f(b);f(c);

    The major dimension can be anything you like, but all the minor dimensions have to match the declaration of the function exactly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    The solution...

    This is what I wanted to do it...
    (I got the solution from another forum... thanks twomers)
    Code:
    #include<stdio.h> 
    
    void print ( int *pnum, int x, int y ) 
    { int j,i;
       for (j=0; j<y; j++, printf("\n") ) 
       { 
          for (i=0; i<x; i++ ) 
          { 
             printf( "%d ", *pnum ); 
    
             *pnum++; 
          } 
       } 
    } 
    
    int main( void ) 
    { 
       int test22[2][2] = {{9, 8}, {7, 6}}; 
       int test23[2][3] = {{9, 8, 7}, {6, 5, 4}}; 
       int test33[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}; 
       int test24[2][4] = {{9, 8, 7, 6}, {5, 4, 3, 2}}; 
    
    
       printf( "Printing 2x2 matrix\n" ); 
       print( &test22[0][0], 2, 2 ); 
        
       printf( "\nPrinting 2x3 matrix\n" ); 
       print( &test23[0][0], 3, 2 ); 
    
       printf( "\nPrinting 3x3 matrix\n" ); 
       print( &test33[0][0], 3, 3 ); 
        
       printf( "\nPrinting 2x4 matrix\n" ); 
       print( &test24[0][0], 4, 2 ); 
    	
       return 0; 
    }
    thanks twomers! ...

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Thanks Nacho that was exactly what i was looking for! This print function can run for any 2D array. But your code contains 1 error it shouldn't be *pnum++ but it should be pnum++ since we are incrementing address by 2 bytes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I Need Help Defining and Calling Functions
    By jonbuckets in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2007, 09:46 AM