数组索引和地址返回相同的值 [英] array index and address return same value

查看:164
本文介绍了数组索引和地址返回相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #包括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, although a and &a print the same value their types are different.

Also what is the type of &a?

Pointer to an array containing three ints , i.e int (*)[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 in printf invokes Undefined Behavior)

这篇关于数组索引和地址返回相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆