Home c Transmission of a two-dimensional array as an argument of the

# Transmission of a two-dimensional array as an argument of the

Author

Date

Category

I can’t figure out the transmission of a two-dimensional array to the function and output it with this function on the screen. And what other ways can one transmit a two-dimensional array into a function?

``````# include & lt; stdio.h & gt;
INT PRINTMAS (INTMAS [3] [4])
{
for (int i = 0; i & lt; 3; i ++)
{
for (int j = 0; j & lt; 4; j ++)
{
PrintF ("% d", intmas [i] [j]);
}
PrintF ("\ n");
}
}
INT MAIN ()
{
INT DANMASSV [3] [4] = {{10,9,98,65},
{8, -9, -4,6},
{15,6,78, -8}};
Printmas (Danmassiv);
}
``````

argument function two-dimensional array

``````int fun (int array [] [])
INT FUN (Int * Array [])
INT FUN (Int ** Array)
``````

may in general, it is best to transmit the size of the array and address of its first element to the function (i.e., to represent this array as one-dimensional function) and independently calculate the desired index when accessing the elements of the array.

For example, for a two-dimensional array:

``````void pri_arr (int ncol, int nlines, int a []) {
int i, j;
for (i = 0; i & lt; nlines; i ++)
For (j = 0; j & lt; ncol || (Puts ("), 0); j ++)
PrintF ("% D", A [I * NCOL + J]);
}
INT MAIN ()
{
Puts ("2 dim");
INT A [3] [4] = {{1, 2, 3, 4},
{11, 12, 13, 14},
{21, 22, 23, 24}};
pri_arr (4, 3, & amp; a [0] [0]);
}
``````

This method works in all SI and C++ compilers.

But in GCC (C, not C++) the same example can be written and more naturally:

``````void pri_arr (int ncol, int nlines, int n [] [NCOL]) {
int i, j;
for (i = 0; i & lt; nlines; i ++)
For (j = 0; j & lt; ncol || (Puts ("), 0); j ++)
PrintF ("% d", a [i] [j]);
}
``````

Please note the number of items in the row of the array must be transmitted before its ad in the list of arguments.

``````int main ()
{
Puts ("2 dim");
INT A [3] [4] = {{1, 2, 3, 4},
{11, 12, 13, 14},
{21, 22, 23, 24}};
pri_arr (4, 3, a);
}
``````

When calling, we transmit an array itself (and not the address of the first element, as in the first example).

The first brackets must be left empty, but all of the following must be fill out by specifying the corresponding size. It is necessary that the compiler can determine the depth of each additional array.

In this way:

``````int printmas (int intmas [] [4]) {...}
``````

GCC (Ubuntu 4.8.4-2BUNTU1 ~ 14.04) 4.8.4

``````# include & lt; stdio.h & gt;
#Define Rows 3.
#Define Cols 4.
/ * Describe the prototype function. Here int (* ar) [col] pointer to an array of 4 elements of type int * /
Void Printmas (int (* AR) [cols], int rows);
INT MAIN (Void)
{
/ * Set and initialize an array * /
INT DANMASSIV [ROWS] [COLS] = {
{10,9,98,65},
{8, -9, -4,6},
{15,6,78, -8}
};
Printf ("The range of values ​​is: \ n");
/ * Call the function by passing it the name of the array as a pointer to 1 element (an array of 4 values ​​of the INT type) and the number of elements in it * /
Printmas (Danmassiv, Rows);
Return 0;
}
/ * function definition * /
Void Printmas (int ar [] [cols], int rows)
{
int r;
int c;
for (r = 0; r & lt; rows; r ++)
{
For (C = 0; C & LT; COLS; C++)
/ * Printing array elements - Ar [R] [c] pointer to each element of the array * /
PrintF ("% d", ar [r] [c]);
PrintF ("\ n");
}
}
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.