Array of pointers to an array of fixed size

后端 未结 9 1809
心在旅途
心在旅途 2020-12-14 06:04

I tried to assign two fixed-size arrays to an array of pointers to them, but the compiler warns me and I don\'t understand why.

int A[5][5];
int B[5][5];
int         


        
9条回答
  •  没有蜡笔的小新
    2020-12-14 06:40

    Either you should declare the third array like

    int A[5][5];
    int B[5][5];
    int ( *C[] )[N][N] = { &A, &B };
    

    that is as an array of pointers to two-dimensional arrays.

    For example

    #include 
    
    #define N   5
    
    void output( int ( *a )[N][N] )
    {
        for ( size_t i = 0; i < N; i++ )
        {
            for ( size_t j = 0; j < N; j++ ) printf( "%2d ", ( *a )[i][j] );
            printf( "\n" );
        }
    }
    
    int main( void )
    {
        int A[N][N] =
        {
            {  1,  2,  3,  4,  5 },
            {  6,  7,  8,  9, 10 },
            { 11, 12, 13, 14, 15 },
            { 16, 17, 18, 19, 20 },
            { 21, 22, 23, 24, 25 }
        };
        int B[N][N] =
        {
            { 25, 24, 23, 22, 21 },
            { 20, 19, 18, 17, 16 },
            { 15, 14, 13, 12, 11 },
            { 10,  9,  8,  7,  6 },
            {  5,  4,  3,  2,  1 }
        };
    
    /*
        typedef int ( *T )[N][N];
        T C[] = { &A, &B };
    */
    
        int ( *C[] )[N][N] = { &A, &B };
    
        output( C[0] );
        printf( "\n" );
    
        output( C[1] );
        printf( "\n" );
    }        
    

    The program output is

     1  2  3  4  5 
     6  7  8  9 10 
    11 12 13 14 15 
    16 17 18 19 20 
    21 22 23 24 25 
    
    25 24 23 22 21 
    20 19 18 17 16 
    15 14 13 12 11 
    10  9  8  7  6 
     5  4  3  2  1 
    

    or like

    int A[5][5];
    int B[5][5];
    int ( *C[] )[N] = { A, B };
    

    that is as an array of pointers to the first elements of two-dimensional arrays.

    For example

    #include 
    
    #define N   5
    
    void output( int ( *a )[N] )
    {
        for ( size_t i = 0; i < N; i++ )
        {
            for ( size_t j = 0; j < N; j++ ) printf( "%2d ", a[i][j] );
            printf( "\n" );
        }
    }
    
    int main( void )
    {
        int A[N][N] =
        {
            {  1,  2,  3,  4,  5 },
            {  6,  7,  8,  9, 10 },
            { 11, 12, 13, 14, 15 },
            { 16, 17, 18, 19, 20 },
            { 21, 22, 23, 24, 25 }
        };
        int B[N][N] =
        {
            { 25, 24, 23, 22, 21 },
            { 20, 19, 18, 17, 16 },
            { 15, 14, 13, 12, 11 },
            { 10,  9,  8,  7,  6 },
            {  5,  4,  3,  2,  1 }
        };
    
    /*
        typedef int ( *T )[N];
        T C[] = { A, B };
    */
    
        int ( *C[] )[N] = { A, B };
    
        output( C[0] );
        printf( "\n" );
    
        output( C[1] );
        printf( "\n" );
    }        
    

    The program output is the same as above

     1  2  3  4  5 
     6  7  8  9 10 
    11 12 13 14 15 
    16 17 18 19 20 
    21 22 23 24 25 
    
    25 24 23 22 21 
    20 19 18 17 16 
    15 14 13 12 11 
    10  9  8  7  6 
     5  4  3  2  1 
    

    depending on how you are going to use the third array.

    Using typedefs (shown in the demonstrative program as commented) ssimplifies the arrays' definitions.

    As for this declaration

    int*** C = {&A, &B};
    

    then in the left side there is declared a pointer of type int *** that is a scalar object while in the right side there is a list of initializers that have different type int ( * )[N][N].

    So the compiler issues a message.

提交回复
热议问题