How to pass a two-dimensional array to a function in C?

前端 未结 3 1345
时光说笑
时光说笑 2021-01-15 07:59

My function prototype is

int** rotate(int **arr, int row, int col, int fl);

where arr is the two dimensional array, row<

相关标签:
3条回答
  • 2021-01-15 08:22

    From the example, I assume that you are using a static definition of arr.

    The Pointer to pointer is not the same as a 2D array.

    Change

    int** rotate(int **arr, int row, int col, int fl);
    

    to

    int** rotate(int arr[][20], int row, int col, int fl);
    

    Note that the no of columns will have to be defined before compilation.

    0 讨论(0)
  • 2021-01-15 08:28

    If your compiler supports variable length arrays then the function declaration can look the following way

    void rotate( size_t row, size_t col, int arr[][col], int fl);
    

    or

    void rotate( size_t row, size_t col, int arr[][col], _Bool fl);
    

    In this case you can use arrays with different sizes.

    Here is a demonstrative program

    #include <stdio.h>
    
    void rotate( size_t row, size_t col, int a[][col], _Bool fl )
    {
        for ( size_t i = 0; i < ( fl ? row : col ); i++ )
        {
            for ( size_t j = 0; j < ( fl ? col : row ); j++ )
            {
                printf( "%d ", a[fl ? i : j][fl ? j : i] );
            }
            putchar( '\n' );
        }
    }
    
    #define N1  3
    
    int main(void) 
    {
        int a[][3] =
        {
            { 1, 2, 3 },
            { 4, 5, 6 }
        };
    
        rotate( sizeof( a ) / sizeof( *a ), N1, a, 0 );
        putchar( '\n' );
    
        rotate( sizeof( a ) / sizeof( *a ), N1, a, 1 );
        putchar( '\n' );
    
        return 0;
    }
    

    Its output is

    1 4 
    2 5 
    3 6 
    
    1 2 3 
    4 5 6 
    

    Otherwise if within the function you are going to create new arrays then the function can look as it is shown in the following demonstrative program.

    #include <stdio.h>
    #include <stdlib.h>
    
    int ** rotate( size_t, size_t, int a[][*], _Bool fl );
    
    int ** rotate( size_t row, size_t col, int a[][col], _Bool fl )
    {
        int **p = malloc( col * sizeof( int * ) );
        for ( size_t i = 0; i < col; i++ )
        {
            p[i] = ( int * )malloc( row * sizeof( int ) );
        }
    
        if ( fl )
        {
            for ( size_t i = 0; i < row; i++ )
            {
                for ( size_t j = 0; j < col; j++ )
                {
                    p[col - j - 1][i] = a[i][j];
                }
            }
        }
        else
        {
            for ( size_t i = 0; i < row; i++ )
            {
                for ( size_t j = 0; j < col; j++ )
                {
                    p[j][i] = a[row - i - 1][j];
                }
            }
        }
    
        return p;
    }
    
    #define M   2
    #define N   3
    
    int main(void) 
    {
        int a[M][N] =
        {
            { 1, 2, 3 },
            { 4, 5, 6 }
        };
    
        int **p = rotate( M, N, a, 0 );
    
        for ( size_t i = 0; i < N; i++ )
        {
            for ( size_t j = 0; j < M; j++ )
            {
                printf( "%d ", p[i][j] );
            }
            putchar( '\n' );
        }
        putchar( '\n' );
    
        for ( size_t i = 0; i < N; i++ )
        {
            free( p[i] );
        }
        free( p );
    
        p = rotate( M, N, a, 1 );
    
        for ( size_t i = 0; i < N; i++ )
        {
            for ( size_t j = 0; j < M; j++ )
            {
                printf( "%d ", p[i][j] );
            }
            putchar( '\n' );
        }
        putchar( '\n' );
    
        for ( size_t i = 0; i < N; i++ )
        {
            free( p[i] );
        }
        free( p );
    
        return 0;
    }
    

    Its output is

    4 1 
    5 2 
    6 3 
    
    3 6 
    2 5 
    1 4 
    
    0 讨论(0)
  • 2021-01-15 08:40

    A pointer to a pointer is different from a pointer to an array. Array-to-pointer decaying can only happen on the left-most side (e.g. int [3][20] to int (*)[20]).

    Change your function declaration to

    int** rotate(int (*arr)[20], int row, int col, int fl);
    

    or more obviously,

    int** rotate(int arr[][20], int row, int col, int fl);
    

    Note you have to fix the size at compile-time.

    0 讨论(0)
提交回复
热议问题