Difference between `a` and `&a` in C++ where `a` is an array

前端 未结 3 1890
一向
一向 2021-02-04 17:00

I am confused about the output of the following code.

#include
#include
using namespace std;
int main()
{
  int a[] = {1,2,3};
           


        
3条回答
  •  野性不改
    2021-02-04 17:41

    An array is not a pointer, but an array decays to a pointer when you try to use it like one. In your case printing the address of the array automatically converts it into a pointer.

    There's little difference between the automatically converted pointer and the one created explicitly with &, except that one is a pointer to a single element while the other is a pointer to the entire array. If you had used &a[0] then they would be identical.

提交回复
热议问题