简体   繁体   English

为什么我们不能直接将二维数组的地址分配给指针?

[英]Why can't we directly assign the address of a 2D array to a pointer?

Why can't we directly assign the address of a 2D array to a pointer?为什么我们不能直接将二维数组的地址分配给指针?

Is there any way we can assign it in a single line, not with the help of a for loop?有什么方法可以在一行中分配它,而不是借助for循环?

Is there any other better approach?还有其他更好的方法吗?

// Array of 5 pointers to an array of 4 ints 

int (*Aop[5])[4];

for(int i = 0;i<5;i++) 
{
    Aop[i] = &arr2[i];  //Works fine
} 
 
//Why this doesn't work int 
int (*Aop[5])[4] = &arr2[5][4]

This declaration本声明

int (*Aop[5])[4];

does not declare a pointer.不声明指针。 It is a declaration of an array of 5 pointers to one-dimensional arrays of the the int[4] .它是 5 个指向int[4]的一维 arrays 的指针的数组的声明。

Arrays do not have the assignment operator. Arrays 没有赋值运算符。 However you could initialize it in its declaration as for example但是,您可以在其声明中对其进行初始化,例如

int (*Aop[5])[4] = { &arr2[0], &arr2[1], &arr2[2], &arr2[3], &arr2[4] };

or或者

int (*Aop[5])[4] = { arr2, arr2 + 1, arr2 + 2, arr2 + 3, arr2 + 4 };

Here is a demonstrative program.这是一个演示程序。

#include <stdio.h>

int main(void) 
{
    enum { M = 5, N = 4 };
    
    int arr2[M][N] =
    {
        { 1, 1, 1, 1 },
        { 2, 2, 2, 2 },
        { 3, 3, 3, 3 },
        { 4, 4, 4, 4 },
        { 5, 5, 5, 5 }
    };
    
    int (*Aop[M])[N] = { arr2, arr2 + 1, arr2 + 2, arr2 + 3, arr2 + 4 };
    
    for ( size_t i = 0; i < M; i++ )
    {
        for ( size_t j = 0; j < N; j++ )
        {
            printf( "%d ", ( *Aop[i] )[j] );
        }
        putchar( '\n' );
    }
    
    return 0;
}

The program output is程序 output 是

1 1 1 1 
2 2 2 2 
3 3 3 3 
4 4 4 4 
5 5 5 5

If you need to declare a pointer to the array arr2 then you should bear in mind that array designators are implicitly converted (with rare exceptions) to pointers to their first elements.如果您需要声明指向数组arr2的指针,那么您应该记住,数组指示符被隐式转换(极少数例外)为其第一个元素的指针。 The array arr2 has elements of the type int[4] .数组arr2具有int[4]类型的元素。 So a pointer to an object of this type will have the type int ( * )[4] .因此,指向此类型的 object 的指针将具有int ( * )[4]类型。 So you can write所以你可以写

int (*Aop )[4] = arr2;

Here is another demonstrative program that uses pointers in for loops to output elements of the array arr2 .这是另一个演示程序,它在 for 循环中使用指向数组arr2的 output 元素的指针。

#include <stdio.h>

int main(void) 
{
    enum { M = 5, N = 4 };
    
    int arr2[M][N] =
    {
        { 1, 1, 1, 1 },
        { 2, 2, 2, 2 },
        { 3, 3, 3, 3 },
        { 4, 4, 4, 4 },
        { 5, 5, 5, 5 }
    };
    
    int (*Aop )[N] = arr2;
    
    for ( int ( *p )[N] = Aop; p != Aop + M; ++p )
    {
        for ( int *q = *p; q != *p + N; ++q )
        {
            printf( "%d ", *q );
        }
        putchar( '\n' );
    }
    
    return 0;
}

Again the program output is再次,程序 output 是

1 1 1 1 
2 2 2 2 
3 3 3 3 
4 4 4 4 
5 5 5 5

These work just fine to declare a pointer to the entire array:这些可以很好地声明指向整个数组的指针:

int (*Ap)[4] = arr2;

int (*Bp)[5][4] = &arr2;

The size of an array only appears in its declaration, and NOT when passing it around.数组的大小只出现在它的声明中,而不是在传递它时出现。 arrs[5][4] is an out-of-bounds array access (out of bounds by a whole row and a whole column, so you can't even use legally it as the past-the-end reference by avoiding lvalue-to-rvalue conversion) arrs[5][4]是一个越界数组访问(超出一整行和一整列的范围,因此您甚至不能通过避免左值来合法地将它用作过去的引用-右值转换)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM