刺激战场
六合彩
贵宾厅
  • 4941阅读
  • 1回复

Question6.3:So what is meant by the "equivalence of the pointers and arrays" in C? [复制链接]

上一主题 下一主题
离线那个谁
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2013-07-08
— 本帖被 tyrone2497谁 执行加亮操作(2013-07-16) —
Q: So what is meant by the ``equivalence of pointers and arrays'' in C?

A: Much of the confusion surrounding arrays and pointers in C can be traced to a misunderstanding of this statement. Saying that arrays and pointers are ``equivalent'' means neither that they are identical nor even interchangeable. What it means is that array and pointer arithmetic is defined such that a pointer can be conveniently used to access an array or to simulate an array. In other words, as Wayne Throop has put it, it's ``pointer arithmetic and array indexing [that] are equivalent in C, pointers and arrays are different.'')

Specifically, the cornerstone of the equivalence is this key definition:
A reference to an object of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.

That is, whenever an array appears in an expression, the compiler implicitly generates a pointer to the array's first element, just as if the programmer had written &a[0]. (The exceptions are when the array is the operand of a sizeof or & operator, or is a string literal initializer for a character array.
See questions 6.23, 6.12, and 1.32, respectively.)

As a consequence of this definition, and in spite of the fact that the underlying arrays and pointers are quite different, the compiler doesn't apply the array subscripting operator [] that differently to arrays and pointers, after all.Given an array a and pointer p, an expression of the form a causes the array to decay into a pointer, following the rule above, and then to be subscripted just as would be a pointer variable in the expression p (although the eventual memory accesses will be different, as explained in question 6.2). If you were to assign the array's address to the pointer:
                                       p = a;
then p[3] and a[3] would access the same element.

This harmony of access explains how pointers can access arrays, serve in their stead as function parameters (see question 6.4), and simulate dynamic arrays (see question 6.14).

See also questions 6.8, 6.10, and 6.14.
离线那个谁

只看该作者 沙发  发表于: 2013-07-09
问题:那么在C语言中“数组与指针等价”是什么意思?
回答:在C语言中,许多围绕数组和指针的疑惑都是起源于这句话的。我们说数组和指针“等价”既不是说他们是一样的,也不是说他们是可互换的。它代表的意思是数组和指针运算符被定义了,然后一个指针就可以用来访问数组或者是模拟数组。就如同Wayne Throop所说,“在C语言中,指针运算和数组的下标是等价的,但是指针与数组是不同的。”
特别地说,等价的基础就是这个关键的定义:
          引用一个T类型的数组的对象,如果出现在表达式中,将会退化成为指向第一个元素的指针(有三种特殊情况)。指针的类型是指向T的指针。
也就是说,当数组出现在表达式中的时候,编译器就会暗中产生一个指针指向数组的第一个元素。就像程序员写的&a[0]。(特殊情况是当数组作为sizeof或者&的运算对象的时候,或者是作为字符数组的字符串初始值的时候)(分别参考问题6.23、6.12和1.32)。
             作为这个定义的结论就是,虽然从本质上来说数组和指针是有很大的不同,但是对于数组下标操作符[]应用于数组和指针时并没有什么不同。我们来看一个数组a和一个指针p,对于形式如a的表达式中,根据上面的规则数组会退化为指针,然后按照指针变量中的表达式p[]那样被下标(尽管它们最终访问内存的方式不同,就如问题6.2中所描述的一样)。如果你将一个数组的地址给了指针:          
               p=a;
那么p[3]和a[3]将访问相同的元素。
这种和谐的访问就解释了指针如何访问数组和替代数组作为函数的参数(参考问题6.4)和模拟动态数组(参考问题6.14)。
参考问题6.8、6.10和6.14。
快速回复
限100 字节
批量上传需要先选择文件,再选择上传
 
上一个 下一个