int* p1;
只是一个指针。当与new []一起使用时,它可以像数组或迭代器一样递增。这很好,但什么是什么样的变量是int * [2]?如在分层int * p2 [2]
int* p2[2];
它看起来应该是一个指向具有两个元素的数组的指针,对吗?但是如果我创建一个两元素数组,我不能指向它。无论如何我无法找到让p2指向某个东西的方法。下面的很多只是尝试不同的作业,但它没有工作,这真的让我感到惊讶。那么什么是int * [2],它与int(*)[2]有什么不同?
int main()
{
int arr[2];
int* p1; //pointer to int, can be used like an array
int* p2[2]; //pointer to an array
p1 = new int[2];
p1 = arr;
//p1 = &arr; //cannot convert ‘int (*)[2]’ to ‘int*’ in assignment
//p2 = &arr; //incompatible types in assignment of ‘int (*)[2]’ to ‘int* [2]’
//p2 = &p1; //incompatible types in assignment of ‘int**’ to ‘int* [2]’
//p2 = new int[2]; //incompatible types in assignment of ‘int*’ to ‘int* [2]’
//p2 = arr; //incompatible types in assignment of ‘int [2]’ to ‘int* [2]’
}
'int * p2 [2];'是一个指向'int'的两个指针的数组。如果你不确定这样的声明是什么意思,请使用[cdecl.org](http://cdecl.ridiculousfish.com/?q=int*+p2%5B2%5D)。 –
当然是。只是其中一个... – roro
如果你想要一个指向数组的指针,那就是'int(* p2)[2];'。请注意,在这种情况下,数组的大小很重要 - 如果(例如)您增加了'p2',它将以2'int'数组的大小增加。 –