gpt4 book ai didi

c - 为什么指针和数组不一样: Need justification on quote

转载 作者:行者123 更新时间:2023-11-30 20:48:51 25 4
gpt4 key购买 nike

我需要以下引用,尤其是粗体文本,才能证明其合理性。

... This would tend to imply that somehow source[i] is the same as *(p+i).

In fact, this is true, i.e wherever one writes a[i] it can be replaced with *(a + i) without any problems.

In fact, the compiler will create the same code in either case. Thus we see that pointer arithmetic is the same thing as array indexing. Either syntax produces the same result. This is NOT saying that pointers and arrays are the same thing, they are not. We are only saying that to identify a given element of an array we have the choice of two syntaxes, one using array indexing and the other using pointer arithmetic, which yield identical results.

这是从pdf中引用的

A TUTORIAL ON POINTERS AND ARRAYS IN C by Ted Jensen Version 1.2 (PDF Version) Sept. 2003 P.No : 19

最佳答案

根据定义,表达式a[i]*(a + i) 是相同的。 C 和 C++ 标准定义表达式语法 a[i] 始终等同于 *(a + i),因此a[i]i[a] 可以互换。

这并没有告诉您有关指针和数组之间关系的任何信息,因为从技术上讲,在 a[i]*(a + i) 中,a 是一个指针类型的表达式[] 运算符左侧采用的是指针表达式,而不是数组表达式; + 运算符仅对指针和整数进行操作,而不对数组和整数进行操作。

您可以使用数组代替a,这仅仅是因为数组类型的表达式可以隐式转换为指针类型的右值表达式。

关于c - 为什么指针和数组不一样: Need justification on quote,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28018877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com