指针算术中数组行为的地址 [英] Address of array behavior in pointer arithmetic

查看:75
本文介绍了指针算术中数组行为的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经阅读了常见问题解答中的相关章节以及

这个主题的其他主题,但是没有找到我问题的确切答案。一位家伙

开发人员在一些代码中写了以下内容:


uint16 arr [10];


uint16 val ;


val = *(& arr + 0);


所以我知道& arr是一个指向数组的指针$ int与arr

,这是在值上下文中使用时指向int的指针。在我的情况下,我的

编译器最终在''val''中存储''arr''的地址,代码为

以上。有人可以解释代码片段中真正发生的事情

以上?换句话说,(& arr + 0)表达式在

类型等条件下的评估结果。


谢谢。

解决方案

joshc写道:

uint16 arr [10];

uint16 val ;

val = *(& arr + 0);
& arr是一个指向10 ints数组的指针
换句话说,(& arr + 0)表达式在
类型等方面的评估结果




(& arr + 0)



& arr

相同
val = *(& arr + 0);



val = arr;

相同并且应该生成同样的警告。


-

pete


> uint16 arr [10];


uint16 val;

val = *(& arr + 0);

所以我知道& arr是一个指向10个int数组的指针,而arr
是在值上下文中使用时指向int的指针。在我的情况下,我的
编译器最终在''val''中存储了''arr''的地址,用于上面的代码
。有人可以解释上面代码片段中真正发生的事情吗?换句话说,(& arr + 0)表达式在
类型等方面的评估结果。

谢谢。




arr是数组的简单指针。当你说& arr它是某种东西

类似于指针的指针。


所以当你试图解除引用(& arr + 0)时它会产生结果到另一个地址。

另外,如果你看看gcc编译器生成的警告


"警告:赋值从指针生成整数而没有强制转换


非常清楚。


如上所述,

val = arr 。但是如果给出数组名称,它将生成数组的起始

地址。


这个是正确的。


main()

{


int val;

int arr [10];


val = **(& arr + 0);


}


谢谢,

迪帕克。

jackdorse写道:

uint16 arr [10];

uint16 val;

val = *(& arr + 0);

所以我知道& arr是一个指向10个整数数组的指针而不是arr
这是在值上下文中使用时指向int的指针。在我的情况下,我的
编译器最终在''val''中存储了''arr''的地址,用于上面的代码
。有人可以解释上面代码片段中真正发生的事情吗?换句话说,(& arr + 0)表达式在
类型等方面的评估结果。

谢谢。



arr是一个简单的数组指针。当你说& arr它类似于指针的指针。

所以当你试图解除引用(& arr + 0)时它会产生一个地址。
另外,如果你看一下gcc编译器生成的警告

警告:赋值使得没有强制转换的指针产生整数

使得这一点非常明确。




So I''ve read the relevant sections in the FAQ and the other threads on
this topic but didn''t find the exact answer to my question. A fellow
developer wrote the following in some code:

uint16 arr[10];

uint16 val;

val = *(&arr + 0);

So I know that &arr is a pointer to an array of 10 ints versus arr
which is a pointer to int when used in value context. In my case my
compiler ended up storing the address of ''arr'' in ''val'' for the code
above. Can someone explain what really happens in the code snippet
above? In other words what the (&arr + 0) expression evaluates to in
terms of type, etc.

Thanks.

解决方案

joshc wrote:

uint16 arr[10];

uint16 val;

val = *(&arr + 0); &arr is a pointer to an array of 10 ints In other words what the (&arr + 0) expression evaluates to in
terms of type, etc.



(&arr + 0)
is the same as
&arr

val = *(&arr + 0);
is the same as
val = arr;
and should generate the same warning.

--
pete


> uint16 arr[10];


uint16 val;

val = *(&arr + 0);

So I know that &arr is a pointer to an array of 10 ints versus arr
which is a pointer to int when used in value context. In my case my
compiler ended up storing the address of ''arr'' in ''val'' for the code
above. Can someone explain what really happens in the code snippet
above? In other words what the (&arr + 0) expression evaluates to in
terms of type, etc.

Thanks.



arr is a simple pointer of the array. when u say &arr it is something
similar to pointer to pointer.

so when u try to dereference (&arr + 0) it results into an address.
Also, if u look at the warning generated by gcc compiler

"warning: assignment makes integer from pointer without a cast"

makes the point very clear.


As said,
val = arr. But if gives array name, it ''ll generate the starting
address of array.

This one ''ll be correct.

main()
{

int val;
int arr[10];

val = **(&arr + 0);

}

Thanks,
Deepak.
jackdorse wrote:

uint16 arr[10];

uint16 val;

val = *(&arr + 0);

So I know that &arr is a pointer to an array of 10 ints versus arr
which is a pointer to int when used in value context. In my case my
compiler ended up storing the address of ''arr'' in ''val'' for the code
above. Can someone explain what really happens in the code snippet
above? In other words what the (&arr + 0) expression evaluates to in
terms of type, etc.

Thanks.



arr is a simple pointer of the array. when u say &arr it is something
similar to pointer to pointer.

so when u try to dereference (&arr + 0) it results into an address.
Also, if u look at the warning generated by gcc compiler

"warning: assignment makes integer from pointer without a cast"

makes the point very clear.




这篇关于指针算术中数组行为的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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