-1
void sort(int [],int);

This is how I usually use a single dimensional array(i.e: int [] ) inside a function declaration statement for programs like sorting and it works fine without any problem

But for my matrix addition program when I use a two dimensional array(i.e:int [][] ) inside a function declaration statement with the similar format void mat(int [][],int [][],int ,int); Iam getting some error messages like:-

1.multidimensional array must have bounds for all dimensions except the first.

2.invalid conversion from 'int (*)[10]' to 'int' [-fpermissive].

So my question is how to write a two dimensional array inside a function declaration statement.

Below I have attached my full Matrix addition program using functions:-

#include<stdio.h> 
void mat(int [][],int [][],int ,int);
int main()
{
    int a[10][10],b[10][10],m,n,i,j;
    printf("Enter the rows and coloumns: ");
    scanf("%d %d",&m,&n);
    printf("\nEnter the elements of 1st Matrix:");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
        scanf("%d",&a[i][j]);
    printf("\nEnter the elements of the 2nd Matrix:");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            scanf("%d",&b[i][j]);
    mat(a,b,m,n);
}
void mat(int a[10][10],int b[10][10],int m,int n)
{
    int i,j,c[10][10];
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
        c[i][j]=a[i][j]+b[i][j];
        printf("\nThe addition of the 2 Matrix is :");
        for(i=0;i<m;i++)
        {
            printf("\n");
            for(j=0;j<n;j++)
            printf("%d",c[i][j]);
        }
}
Sparkzz
  • 367
  • 5
  • 14
  • The function prototype should be a copy/paste of the function definition, with a semicolon tacked on the end. – user3386109 Feb 11 '16 at 19:08
  • No function prototypes will be having only the datatypes whereas function definition will have both the datatype with the variable name – Sparkzz Feb 11 '16 at 19:19
  • There are no two- (or any higher up) dimensitonal arrays in C. Instead, you can have arrays of arrays, which is not the same. – SergeyA Feb 11 '16 at 19:22
  • @SairamD You can choose to do it that way if you wish, but eventually you'll realize why that's a bad choice. – user3386109 Feb 11 '16 at 21:25

3 Answers3

1

The prototype

void mat(int [][],int [][],int ,int);  

should be

void mat(int [][10],int [][10],int ,int);  

You must have to specify the higher dimensions. Other way around, the above prototype is equivalent to

void mat(int (*)[10],int (*)[10],int ,int);  

int (*)[10] is a type (pointer to an array of 10 int) and without size 10 it is of incomplete type.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

In C, when you declare an array, all dimensions of the array except possibly the last must be specified. In other words, you can write a function like this:

void iTakeAnArray(int[]);

or this:

void iTakeAnotherArray(int[][5]);

but you can't write

void oopsNotAllowed(int[][]);

The reason for this has to do with how the C compiler typically represents arrays. In order to do a lookup into a 2D array, the compiler needs to know how many elements are along one dimension of the array so that it knows the ultimate memory offset to locate when you perform an array access.

There isn't a clean way to get around this other than to

  1. have the function take in a 2D array where you fix the size in advance, or
  2. simulate a 2D array with a 1D array.

For that second option, you can represent an array of dimensions m x n with a single 1D array of size m x n. To look up element arr[i][j]. you look at position i * n + j in the array. It's a bit hacky, but it works!

user3386109
  • 34,287
  • 7
  • 49
  • 68
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

dear you can not initialize two dimensional array in such a fashion a[][] in C.How will it get to know that how many columns are there.

  • Instead you can do a[][10].
  • But my personal preference is to use pointers like (*a)[10]. As it makes the things easy to visualize.
Kevin Pandya
  • 1,036
  • 1
  • 9
  • 12