c - Matrix as function parameter -


why when using matrix parameter of function must specify number of columns don't have indicate number of rows?

i assume you're talking function definition like

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

this requires little background...

except when operand of sizeof or unary & operators, or string literal being used initialize array in declaration, expression of type "n-element array of t" converted ("decay") expression of type "pointer t", , value of expression address of first element of array. so, given declaration

int arr[10]; 

the expression arr have type "10-element array of int"; unless expression operand of sizeof or unary & operators, converted ("decay") expression of type "pointer int". if pass array function, like

blurga( arr ); 

what blurga receives pointer int, not array of int. can define blurga as

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

the formal parameter ap has type "pointer int". note can use [] operator on pointer expressions array expressions1.

now, in context of formal parameter declaration, t a[n] , t a[] interpreted t *a; 3 declare a pointer t, not array of t. can rewrite function definition as

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

again, int ap[] interpreted same int *ap.

now let's @ two-dimensional array:

int blah[10][10]; 

the expression blah has type "10-element array of 10-element array of int". going conversion rule above, expression blah decay expression of type "pointer 10-element array of int", or int (*)[10]. if pass blah function, function receives pointer array, not array of arrays:

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

as before t a[] interpreted t *a, can write declaration as

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

just remember barr pointer type, not array type.


1. in fact, a[i] defined result of *(a + i); given pointer value a, offset i elements address , dereference result. basically, conversion rule still applies; array expression a being converted pointer expression, , pointer expression [] being applied to.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -