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?
指向包含三个int
s 的数组的指针,即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
调用未定义的行为)