Home>

As for the presentation code, I want to pass a two-dimensional array x as an argument to the print_t ();function. What should I do? I could do it in one dimension, but I don't know what to do when it comes to a two-dimensional array.

#include "stdio.h"


void print_ (int ** t)
{
    printf ("% d \ n", t [0]);
}

int main ()
{
    int x1 [3] = {1,2,3};
    int x [3] [3] {
        {1,2,3},
        {4,5,6},
        {7,8,9},
    };
    print_ (x);

    return 0;
}
c++
  • Answer # 1

    When passing a two-dimensional array, the number of outer elements cannot be omitted.
    You need to do the following:

    #include "stdio.h"
    void print_array (int array [] [3], size_t rows)
    {
        for (size_t i = 0;i<rows;++ i) {
            for (size_t j = 0;j<3;++ j) {
                printf ("% d", array [i] [j]);
            }
            printf ("\ n");
        }
    }
    int main ()
    {
        int array [3] [3] {
            {one two Three},
            {4, 5, 6},
            {7, 8, 9},
        };
        print_array (array, 3);
        return 0;
    }
    Example rewritten with vector

    For C ++, it's better to use std :: vector instead of an array in C.

    #include<iostream>
    #include<vector>
    using Mat = std :: vector<std :: vector<int >>;
    void print_array (const Mat&mat)
    {
        for (size_t i = 0;i<mat.size ();++ i) {
            for (size_t j = 0;j<mat [0] .size ();++ j) {
                std :: cout<<mat [i] [j]<<"";
            }
            std :: cout<<std :: endl;
        }
    }
    int main ()
    {
        Mat mat {
            {one two Three},
            {4, 5, 6},
            {7, 8, 9},
        };
        print_array (mat);
        return 0;
    }

  • Answer # 2

    * The tag is C ++, but since the code was in C language, the answer is based on C language.

    The print_ function can only receive the start address of the x array.
    From the print_ function, the number of elements in the array is unknown.
    Therefore, it is necessary to explicitly specify the number of elements on the function side.

    When specifying with an argument ①

    void print_ (int t [3] [3])
    {
        // x [2] [2]
        printf ("% d \ n", t [2] [2]);
    }

    When specifying with an argument ②
    The number of lines can be omitted.

    void print_ (int t [] [3])
    {
        // x [2] [2]
        printf ("% d \ n", t [2] [2]);
    }

    When finding the element position by calculation.

    void print_ (int * t)
    {
        // x [2] [2]
        printf ("% d \ n", t [2 * 3 + 2]);
    }

    In either case, it is a good idea to define the number of elements with a macro.

    #define ROWS 3
    #define COLS 3
    void print_ (int t [ROWS] [COLS])
    {
        // x [2] [2]
        printf ("% d \ n", t [2] [2]);
    }
    void print_ (int * t)
    {
        // x [2] [2]
        printf ("% d \ n", t [2 * COLS + 2]);
    }
    int main ()
    {
        int x [ROWS] [COLS] {
            {1,2,3},
            {4,5,6},
            {7,8,9},
        };
        print_ (x);
        return 0;
    }