16

I have an issue passing a matrix to a function in C. There is the function I want to create:

void ins (int *matrix, int row, int column);

but I noticed that in contrast to the vectors, matrix give me an error. How can I pass my matrix to a function so?

EDIT --> there is the code:

// Matrix

#include <stdio.h>
#define SIZE 100

void ins (int *matrix, int row, int column);
void print (int *matrix, int row, int column);

int main ()
{
    int mat[SIZE][SIZE];
    int row, col;

    printf("Input rows: ");
    scanf  ("%d", &row);
    printf("Input columns: ");
    scanf  ("%d", &col);

    printf ("Input data: \n");
    ins(mat, row, col);

    printf ("You entered: ");
    print(mat, row, col);

    return 0;
}

void ins (int *matrix, int row, int column);
{
    int i, j;

    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            printf ("Row %d column %d: ", i+1, j+1);
            scanf  ("%d", &matrix[i][j]);
        }
    }
}

void print (int *matrix, int row, int column)
{
    int i;
    int j;

    for(i=0; i<row; i++)
    {
        for(j=0; j<column; j++)
        {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Lc0rE
  • 2,236
  • 8
  • 26
  • 34
  • How much are the diemensions of your matrix? Is it 1D or 2D? –  Jul 11 '12 at 08:34
  • Possible duplicate http://stackoverflow.com/questions/546860/passing-arrays-and-matrices-to-functions-as-pointers-and-pointers-to-pointers-in/11274267#11274267 – Ulterior Jul 11 '12 at 08:40

5 Answers5

37

You need to pass a pointer with as much levels of indirection (*) as the number of dimensions of your matrix.

For example, if your matrix is 2D (e.g. 10 by 100), then:

void ins (int **matrix, int row, int column);

If you have a fixed dimension (e.g. 100), you can also do:

void ins (int (*matrix)[100], int row, int column);

or in your case:

void ins (int (*matrix)[SIZE], int row, int column);

If both your dimensions are fixed:

void ins (int matrix[10][100], int row, int column);

or in your case:

void ins (int matrix[SIZE][SIZE], int row, int column);
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Thanks. If i have a constant dimension (look my edits) defined SIZE, how need do i change the code? – Lc0rE Jul 11 '12 at 08:38
  • 2
    It is not possible to pass a 2D array as in the OP's example through "a pointer with as much levels of indirection (*) as the number of dimensions of your matrix". That part of the answer is completely incorrect. – AnT stands with Russia Nov 14 '21 at 20:04
14

If you have a modern C compiler you can do the following for 2D matrices of any sizes

void ins (size_t rows, size_t columns, int matrix[rows][columns]);

Important is that the sizes come before the matrix, such that they are known, there.

Inside your function you then can access the elements easily as matrix[i][j] and the compiler is doing all the index calculations for you.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
2

it would also possible to leave the first dimension empty, the same as (*matrix):

void ins (int matrix[][100], int row, int column);
asam
  • 359
  • 3
  • 12
0

Much better way to use malloc function and create dynamically allocated array and do whatever you want to do using 2d array:

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>

void fun(int **arr,int m,int n)
{
    int i, j;
    for (i = 0; i < m; i++)
    {
    for (j = 0; j < n; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }

}

int main()
{
    int i,j,m,n;
    printf("enter order of matrix(m*n)");
    scanf("%d*%d",&m,&n);
    int **a=(int **)malloc(m*sizeof(int));
    for(i=0;i<n;i++)
        a[i]=(int *)malloc(n*sizeof(int));

    fun(a,m,n);
    for (i = 0; i < m; i++)
    {
    for (j = 0; j < n; j++)
        {
        printf("%d ", a[i][j]);
        }
        printf("\n");

    }
    return 0;
}

 output:
     #include <stdio.h>
#include <stdlib.h>
#include <stdio.h>

void fun(int **arr,int m,int n)
{
    int i, j;
    for (i = 0; i < m; i++)
    {
    for (j = 0; j < n; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }

}

int main()
{
    int i,j,m,n;
    printf("enter order of matrix(m*n)");
    scanf("%d*%d",&m,&n);
    int **a=(int **)malloc(m*sizeof(int));
    for(i=0;i<n;i++)
        a[i]=(int *)malloc(n*sizeof(int));

    fun(a,m,n);
    for (i = 0; i < m; i++)
    {
    for (j = 0; j < n; j++)
        {
        printf("%d ", a[i][j]);
        }
        printf("\n");

    }
    return 0;
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
-1

You can try this as well:

void inputmat(int r,int c,int arr[r * sizeof(int)][c * sizeof(int)])
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61