0

Why when using a matrix as a parameter of a function we must specify the number of columns but we don't have to indicate number of rows?

user3482381
  • 83
  • 1
  • 7
  • 1
    Can't say anything for sure without code. – Sergey L. Apr 14 '14 at 16:55
  • 1
    http://www.c-faq.com/aryptr/ – Carl Norum Apr 14 '14 at 16:59
  • http://stackoverflow.com/questions/16004668/c-allocating-a-matrix-in-a-function/27366086#27366086 Above you will find a program that I have made with functions allocating and manipulating matrices in any possible way for C (gcc C11/C99). – 42n4 Dec 08 '14 at 20:20

1 Answers1

1

I assume you're talking about a function definition like

void foo( int matrix[][COLS] ) { ... }

This requires a little background...

Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array. So, given the declaration

int arr[10];

the expression arr will have type "10-element array of int"; unless this expression is the operand of the sizeof or unary & operators, it will be converted ("decay") to an expression of type "pointer to int". So if we pass that array to a function, like

blurga( arr );

what blurga receives is a pointer to int, not an array of int. So we can define blurga as

void blurga( int *ap ) 
{ 
  // do something with ap[i]
}

The formal parameter ap has type "pointer to int". Note that you can use the [] operator on pointer expressions as well as array expressions1.

Now, in the context of a formal parameter declaration, T a[N] and T a[] are interpreted as T *a; all three declare a as a pointer to T, not an array of T. So we can rewrite that function definition as

void blurga( int ap[] )
{
  // do something with ap[i]
}

Again, int ap[] is interpreted exactly the same as int *ap.

Now let's look at a two-dimensional array:

int blah[10][10];

The expression blah has type "10-element array of 10-element array of int". Going by the conversion rule above, the expression blah will decay to an expression of type "pointer to 10-element array of int", or int (*)[10]. So if we pass blah to a function, what the function receives is a pointer to an array, not an array of arrays:

void bletch( int (*barr)[10] );

As before T a[] is interpreted as T *a, so we can write that declaration as

void blech( int barr[][10] ); // barr[] == (*bar)

Just remember that barr is a pointer type, not an array type.


1. In fact, a[i] is defined as the result of *(a + i); given a pointer value a, we offset i elements from that address and dereference the result. So basically, the conversion rule still applies; the array expression a is being converted to a pointer expression, and that pointer expression is what the [] is being applied to.
John Bode
  • 119,563
  • 19
  • 122
  • 198