C ++ and then explore the two-dimensional array of name

#include <iostream>

int main()
{
    
    int d2a[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

    //输出 0,1,2,3,4,5,6,7,8,9,10,11
    for (size_t i = 0; i < 3; i++)
    {
        for (size_t j = 0; j < 4; j++)
        { 
            STD :: COUT << D2A [I] [J] << " , " ; 
        } 
    } 

    // output 0077FD04,0077FD04,0077FD14,0077FD24
     // visible two-dimensional array is the name of the first address data, d2a [0], d2a [1], d2a [2] are the first row, second row, the third row of the first address 
    STD :: COUT D2A << << " , " << D2A [ 0 ] << " , " << D2A [ . 1 ] << " , " << D2A [ 2 ] << STD :: endl; 

    // the most intuitive way all of the elements sequentially output
     // output 0,1,2,3,4,5,6,7 , 8,9,10,11 
    you * pa = ( you *) D2A;
    for (size_t I = 0 ; I < 12 is ; I ++ ) 
    { 
        STD :: COUT << * (I + PA) << " , " ; 
    } 
    
    // two-dimensional array of output when the pointer name hardest to understand
     // output 0,1,2,3,4,5,6,7,8,9,10,11 
    for (size_t I = 0 ; I < 12 is ; I ++ ) 
    { 
        STD :: COUT << * (* + I D2A ) << " , " ; 
        
        // wrong way, although the first two-dimensional array address points to an array, but is equivalent to the row pointer, d2a + 0 = d2a [0 ], d2a + 1 = d2a [1], d2a + 2 = d2a [2]
         // STD :: COUT << * (I + D2A) << ",";

    }


    @With an understanding of the essence of a two-dimensional array name, it is easy to understand the following 
    char STR [] = " ab & " ;
     char (* PX) [ . 3 ] = & STR; 

    ( * PX) [ 0 ] = ' A ' ; 
    ( PX *) [ . 1 ] = ' B ' ; 
    ( * PX) [ 2 ] = ' C ' ; 
}

 

Guess you like

Origin www.cnblogs.com/timeObjserver/p/12002918.html