数组索引和地址返回相同的值 [英] array index and address return same value
问题描述
#包括LT&;&stdio.h中GT;
诠释主要(无效){
诠释一个[3] = {1,2,3};
的printf(\\ n \\ t%U%U%U \\ t \\ n,一,&安培; A,&安培A + 1);
返回0;
}
现在我不知道为什么一个和&安培;返回相同的值,什么是推理和它背后的实际应用?还什么是与放大器的类型;并且可能我也做及(&安培; A)?
现在我不知道为什么一个和&安培;返回值相同,究竟是推理
块引用>
A
是衰减到指针数组的第一个元件的阵列的名称。&放大器;一个
不过是阵列本身的地址,虽然A
和&安培;一个
打印相同的值的类型不同。
还有什么是与放大器的类型;
?
块引用>指向包含一个阵3
INT
S,即INT(*)[3]
可能我也做及(&安培; A)?
块引用>没有,运营商的地址要求其操作数是一个左值。数组名称是一个不可修改的左值那么
&放大器;一个
是合法的,但及(&安培; A)
是没有。印刷型
&放大器;一个
(仅限于C ++ )的#include<&所属类别GT;
#包括LT&;&iostream的GT;诠释的main()
{
诠释一个[] = {1,2,3};
性病::法院LT&;< typeid的(安培; A)。名称();
}P.S:
使用
%P
打印地址(使用的printf
格式不正确说明调用未定义行为)格式说明#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; }
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) ?
解决方案Now i don't get why a and &a return the same value, what is the reasoning
a
is the name of the array that decays to pointer to the first element of the array.&a
is nothing but the address of the array itself, althougha
and&a
print the same value their types are different.Also what is the type of &a?
Pointer to an array containing three
int
s , i.eint (*)[3]
could i also do &(&a) ?
No, address of operator requires its operand to be an lvalue. An array name is a non-modifiable lvalue so
&a
is legal but&(&a)
is not.Printing type of
&a
(C++ Only)#include <typeinfo> #include <iostream> int main() { int a[]={1,2,3}; std::cout<< typeid(&a).name(); }
P.S:
Use
%p
format specifier for printing address(using incorrect format specifier inprintf
invokes Undefined Behavior)这篇关于数组索引和地址返回相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!