I can not figure out the transfer of a two-dimensional array to a function, and its output by this function on the screen. And what other ways can you pass a two-dimensional array to a function?

#include <stdio.h> int printmas (int intmas[3][4]) { for(int i=0; i<3; i++) { for(int j=0; j<4; j++) { printf("%d ", intmas[i][j]); } printf("\n"); } } int main () { int danmassiv [3][4]={{10,9,98,65}, {8,-9,-4,6}, {15,6,78,-8}}; printmas(danmassiv); } 
  • What exactly is unclear and / or not working? And what side is C ++ here? - PinkTux
  • does not specifically output the given array and I cannot figure out how to send it to the function - Tar
  • I copied this code one to one. Everything is perfectly going, working, transmitting and outputting. The compiler is gcc 4.8.4. By the way, on the page with a question (that is, the same), on the right, there is a list of related questions, under the heading "Related". There are a lot of things on this topic, look. - PinkTux
  • thanks for the advice - Tar
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

4 answers 4

The argument function is a two-dimensional array.

 int fun(int array[][]) int fun(int * array[]) int fun(int ** array) 

    In the general case, it may be best to pass the dimensions of the array and the address of its first element to the function (that is, to represent this array as one-dimensional in the function) and independently calculate the required 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 < nlines; i++) for (j = 0; j < 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, &a[0][0]); } 

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

    But in gcc (C, not C ++), the same example can be written in a more natural way:

     void pri_arr (int ncol, int nlines, int a[][ncol]) { int i, j; for(i = 0; i < nlines; i++) for (j = 0; j < ncol || (puts(""), 0); j++) printf("%d ", a[i][j]); } 

    Note that the number of elements in an array string must be passed before it is declared 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 pass the 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 filled in, indicating the appropriate size. This is necessary so that the compiler can determine the depth of each additional array.

      In this way:

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

        gcc (Ubuntu 4.8.4-2ubuntu1 ~ 14.04) 4.8.4

         #include <stdio.h> #define ROWS 3 #define COLS 4 /*ΠΎΠΏΠΈΡˆΠΈΡ‚Π΅ ΠΏΡ€ΠΎΡ‚ΠΎΡ‚ΠΈΠΏ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ. Π—Π΄Π΅ΡΡŒ int (*ar)[COL] ΡƒΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒ Π½Π° массив ΠΈΠ· 4 элСмСнтов Ρ‚ΠΈΠΏΠ° int */ void printmas(int (*ar)[COLS] , int rows); int main(void) { /*Π·Π°Π΄Π°ΠΉΡ‚Π΅ ΠΈ ΠΏΡ€ΠΎΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠΉΡ‚Π΅ массив*/ int danmassiv [ROWS][COLS]={ {10,9,98,65}, {8,-9,-4,6}, {15,6,78,-8} }; printf("Массив Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ ΠΈΠΌΠ΅Π΅Ρ‚ Π²ΠΈΠ΄:\n"); /*Π²Ρ‹Π·ΠΎΠ²ΠΈΡ‚Π΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ, ΠΏΠ΅Ρ€Π΅Π΄Π°Π² Π΅ΠΉ имя массива ΠΊΠ°ΠΊ ΡƒΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒ Π½Π° 1 элСмСнт(массив ΠΈΠ· 4 Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ Ρ‚ΠΈΠΏΠ° int) ΠΈ ΠΊΠΎΠ»-Π²ΠΎ элСмСнтов Π² Π½Π΅ΠΌ */ printmas(danmassiv, ROWS); return 0; } /* ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ */ void printmas(int ar[][COLS], int rows) { int r; int c; for (r = 0; r < rows; r++) { for (c = 0; c < COLS; c++) /* ΠΏΠ΅Ρ‡Π°Ρ‚ΡŒ элСмСнтов массива,- ar[r][c] ΡƒΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒ Π½Π° ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ элСмСнт массива */ printf("%d", ar[r][c]); printf("\n"); } } 
        • It is important for you to understand that, in general, arrays and pointers are closely related to each other. If ar is an array, then the expressions ar [i] and * (ar + i) are equivalent. As soon as it becomes clear, everything else will fall into place. Good luck. - dio4