Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My function prototype is

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

where arr is the two dimensional array, row and col is the number of row and columns of the 2D array respectively, fl is a flag variable. If the value of fl is 0 then the array will be rotated right, if fl is 1 then the array will be rotated left.

I have called the function as follows:

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

But I got one warning and one note

[Warning] passing argument 1 of 'rotate' from incompatible pointer type.

[Note] expected 'int **' but argument is of type 'int (*)[20]' 
like image 204
SB56 Avatar asked Dec 09 '25 17:12

SB56


2 Answers

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.

like image 92
iBug Avatar answered Dec 11 '25 08:12

iBug


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 
like image 22
Vlad from Moscow Avatar answered Dec 11 '25 09:12

Vlad from Moscow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!