Let's say I have 2D array of N rows and M columns, how do pass one of the rows of this array to a function and the function can use this row as a 1D array? Thanks.
Let's say I have 2D array of N rows and M columns, how do pass one of the rows of this array to a function and the function can use this row as a 1D array? Thanks.
Enjoy.Code:void foo( data_type array[SIZE] ) { } ... data_type array[ROWS][SIZE]; foo( array[x] );
Quzah.
Hope is the first step on the road to disappointment.
>>>void foo( data_type array[SIZE] )
when defining this function, do I must specify the array size as SIZE (here it corresponds to number of columns) ?
Passing a row to it is actually passing a pointer to the first element in whatever row. The size specifier was really just for illustration. Consider the following:
This should run through the list and stop when it hits the null (the zero).Code:#include <stdio.h> #define SIZE 10 void foo( char array[] ) { printf("array is: %s\n", array ); } int main( void ) { char mdarray[][SIZE] = { "foo", "bar", "barfoo", "fweeeeee", "the end", 0 }; int x; for( x = 0; mdarray[x][0]; x++ ) foo( array[x] ); return 0; }
Quzah.
Hope is the first step on the road to disappointment.
Way back when...quzah wrote:
was array[x] supposed to be mdarray[x]?Code:for( x = 0; mdarray[x][0]; x++ ) foo( array[x] ); return 0;
Regards,
Dave
"The thousand natural bugs that code is heir to" Codespeare
Yes. Way to necro a six year old thread!
Quzah.
Hope is the first step on the road to disappointment.