how
is it like
void function (matrix[][])
and calling it
function (matrix[10][10]);
??
Printable View
how
is it like
void function (matrix[][])
and calling it
function (matrix[10][10]);
??
try it and see :)
but yes its along the lines of
type arrayname( type [][] )
standard 2d-array function prototype.
More like
Code:#define ROWS 10
#define COLS 10
void foo(char a[][COLS])
{
}
int main(void)
{
char myarray[ROWS][COLS];
foo(myarray);
return 0;
}
why do u only add the number inside the last brackets only?
Because that's the way C works. If you specify one dimension, you have to specify every dimension to the right of it. You always have to specify the last one.Quote:
Originally posted by revelation437
why do u only add the number inside the last brackets only?
Quzah.
The compiler needs to know the array size in order to do pointer arithmatic. In this snippet:
>>void foochar(char name[]);
the compiler knows that the size of each element in the array is sizeof(char). So when you do name[1], you are adding sizeof(char) to name.
In this example:
>>void foochar(char name[][10]);
the compiler knows the size of name[0][0] is sizeof(char) (as above) and name[0] is size 10*sizeof(char). If you didn't specify the array size in the second brackets, the size equation becomes x*sizeof(char) and your compiler will complain (mine says something like "Size of the type 'signed char[]' is unknown or zero").
Code:#include <stdio.h>
int main(int argc, char *argv[])
{
char a[10][10][10];
printf ("sizeof(a[0][0][0]) = %d\n"
"sizeof(a[0][0]) = %d\n"
"sizeof(a[0]) = %d\n",
sizeof(a[0][0][0]),
sizeof(a[0][0]),
sizeof(a[0])
);
return(0);
}
/*
Output:
sizeof(a[0][0][0]) = 1
sizeof(a[0][0]) = 10
sizeof(a[0]) = 100
*/