0

For functions:

void fn1(int M[size][size],int n,int m){
    ....... 
}

OR

 void fn1(int M[][size],int n,int m){ 
    .......
 }

is the declaration of function What should be the function declaration at the beginning?

The declaration :

void fn1(int **M,int n,int m);

showing error due to mismatch in int **M and int M[][].

Then what should be the proper form?

sb15
  • 313
  • 5
  • 18
  • 1
    Both are correct , as per , but in first `2-d` arrays size if fixed , i.e `size x size` . – ameyCU Oct 31 '15 at 16:33
  • Either can work; they're equivalent as `size` must be a constant, I think. If you want the array to be of size m by n then you must use `void fn1(int m, int n, int M[m][n])` in C99 or later; you don't get the choice to do this in C89/C90. Note that the array size parameters must appear before the array itself. – Jonathan Leffler Oct 31 '15 at 16:34
  • @ameyCU: although the size of the second dimension is fixed at `size`, the size of the first dimension could be anything using either notation; the compiler won't enforce any bounds checking and passing and using a bigger (or smaller) array won't cause any trouble as long as you stay within the bounds of the actual array that is passed. – Jonathan Leffler Oct 31 '15 at 16:37
  • @JonathanLeffler Yes , I agree Sir ,in that comment I referred to array in first definition of function . – ameyCU Oct 31 '15 at 16:37
  • @JonathanLeffler Ohh ,wait . So if `size` is `10` and I pass it an array of `int m[11][11]` it would be fine in first definition? – ameyCU Oct 31 '15 at 16:39
  • Yup, as long as you don't use the array elements that aren't part of the actual array parameter. – Jonathan Leffler Oct 31 '15 at 16:41
  • @all I edited the post. – sb15 Oct 31 '15 at 16:43
  • @JonathanLeffler Sir , I was talking about this definition `void fn1(int M[size][size],int n,int m)` where if I define `size=10` , and pass it an array of `m[11][11]` , then compiler generates an error . In this context was ,my first comment about . – ameyCU Oct 31 '15 at 16:45
  • A good C++-ish declaration would be `void fn1(Matrix& matrix, int n, int m)`, with `Matrix` being a user-defined template class. – Christian Hackl Oct 31 '15 at 16:54

0 Answers0