How to initialize all members of an array to the same value?

后端 未结 23 1892
清歌不尽
清歌不尽 2020-11-21 04:34

I have a large array in C (not C++ if that makes a difference). I want to initialize all members of the same value.

I could swear I

23条回答
  •  忘了有多久
    2020-11-21 05:26

    Nobody has mentioned the index order to access the elements of the initialized array. My example code will give an illustrative example to it.

    #include 
    
    void PrintArray(int a[3][3])
    {
        std::cout << "a11 = " << a[0][0] << "\t\t" << "a12 = " << a[0][1] << "\t\t" << "a13 = " << a[0][2] << std::endl;
        std::cout << "a21 = " << a[1][0] << "\t\t" << "a22 = " << a[1][1] << "\t\t" << "a23 = " << a[1][2] << std::endl;
        std::cout << "a31 = " << a[2][0] << "\t\t" << "a32 = " << a[2][1] << "\t\t" << "a33 = " << a[2][2] << std::endl;
        std::cout << std::endl;
    }
    
    int wmain(int argc, wchar_t * argv[])
    {
        int a1[3][3] =  {   11,     12,     13,     // The most
                            21,     22,     23,     // basic
                            31,     32,     33  };  // format.
    
        int a2[][3] =   {   11,     12,     13,     // The first (outer) dimension
                            21,     22,     23,     // may be omitted. The compiler
                            31,     32,     33  };  // will automatically deduce it.
    
        int a3[3][3] =  {   {11,    12,     13},    // The elements of each
                            {21,    22,     23},    // second (inner) dimension
                            {31,    32,     33} };  // can be grouped together.
    
        int a4[][3] =   {   {11,    12,     13},    // Again, the first dimension
                            {21,    22,     23},    // can be omitted when the 
                            {31,    32,     33} };  // inner elements are grouped.
    
        PrintArray(a1);
        PrintArray(a2);
        PrintArray(a3);
        PrintArray(a4);
    
        // This part shows in which order the elements are stored in the memory.
        int * b = (int *) a1;   // The output is the same for the all four arrays.
        for (int i=0; i<9; i++)
        {
            std::cout << b[i] << '\t';
        }
    
        return 0;
    }
    

    The output is:

    a11 = 11                a12 = 12                a13 = 13
    a21 = 21                a22 = 22                a23 = 23
    a31 = 31                a32 = 32                a33 = 33
    
    a11 = 11                a12 = 12                a13 = 13
    a21 = 21                a22 = 22                a23 = 23
    a31 = 31                a32 = 32                a33 = 33
    
    a11 = 11                a12 = 12                a13 = 13
    a21 = 21                a22 = 22                a23 = 23
    a31 = 31                a32 = 32                a33 = 33
    
    a11 = 11                a12 = 12                a13 = 13
    a21 = 21                a22 = 22                a23 = 23
    a31 = 31                a32 = 32                a33 = 33
    
    11      12      13      21      22      23      31      32      33
    

提交回复
热议问题