array index and address return same value

n0n*_*hun 8 c c++ arrays pointers

#include<stdio.h>
int main(void) {
  int a[3] = {1,2,3};
  printf("\n\t %u %u %u \t\n",a,&a,&a+1);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Now i don't get why a and &a return the same value, what is the reasoning and the practical application behind it? Also what is the type of &a and could i also do &(&a) ?

Pra*_*rav 10

现在我不明白为什么a和&a返回相同的值,是什么原因

a是衰减指向数组第一个元素的数组的名称. &a只不过是数组本身的地址,尽管它们的类型不同a,&a打印相同的值.

还有什么类型的&a?

指向包含三个ints 的数组的指针,即int (*)[3]

我可以做&(&a)吗?

不,运算符的地址要求其操作数为左值.数组名称是不可修改的左值,因此&a是合法的但&(&a)不是.

打印类型&a(仅限C++)

#include <typeinfo>
#include <iostream>

int main()
{
   int a[]={1,2,3};
   std::cout<< typeid(&a).name();
}
Run Code Online (Sandbox Code Playgroud)

PS:

使用%p格式说明符打印地址(使用不正确的格式说明符printf调用未定义的行为)

  • @ n0nChun:对于一个类型为"T"的n个元素的数组,第一个元素的地址具有类型"指向T"的指针.整个数组的地址有类型''指向n'类型的元素的数组'. (2认同)
  • @ n0nChun:如果你说`int i;`你没有声明`i`作为指向`int`的指针,但是`&i`是指向int的指针;) (2认同)