什么是阵列和与放大器之间的区别;阵? [英] What's the difference between array and &array?

查看:124
本文介绍了什么是阵列和与放大器之间的区别;阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设

int array[16];

有一个名为阵列到指针转换标准转换,所以阵列将被隐式转换为类型为int * ,但为什么&放大器;阵列等于阵列

There is standard conversion called array-to-pointer conversion, so array would be converted implicitly to type int*, but why &array is equal to array?

例如,

int array[16];
void *p = array;
void *q = &array;
printf("%p\n", p);
printf("%p\n", q);

这会发出相同的地址,也没有编译错误。

This will give out the same address and no compiling error.

为什么?

推荐答案

这是多么的数组名的行为在 C

数组变量重新$ P $的名称psents数组的第一元素的地址

The name of the array variable represents the address of the first element of the array.

所以,

void *p = array;   //array name, gives address of the  first element.

void *q = &array;  //adress-of-array name, also gives address of the first element
                   // actually &array is of type int (*)[16] which is decayed to int * 
                   // and casted to void * here

P.S。均匀,FWIW,

 void *r = &array[0]; //address of the first element

也将给你相同的地址。

will also give you the same address.

这将给出相同的地址,也没有编译错误。

他们的确是相同的值,在这里,编译器无关的的尖叫声的约。

They are indeed same value, and here compiler has nothing to scream about.

注意点:一旦你的地址(ES)分配给空指针,你将失去的键入的与之相关的信息

Point to note: once you assign the address(es) to a void pointer, you'll lose the type information associated with them.

这篇关于什么是阵列和与放大器之间的区别;阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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