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

Question 6.8:Pratically speaking,what is the difference between arrays and pointers? [复制链接]

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

只看楼主 倒序阅读 使用道具 楼主  发表于: 2013-07-10
— 本帖被 tyrone2497谁 执行加亮操作(2013-07-19) —

Q: Practically speaking, what is the difference between arrays and pointers?
A: An array is a single, preallocated chunk of contiguous elements (all of the same type), fixed in size and location. A pointer is a reference to any data element (of a particular type) anywhere. A pointer must be assigned to point to space allocated elsewhere, but it can be reassigned (and the space, if derived from malloc, can be resized) at any time. A pointer can point to an array, and can simulate (along with malloc) a dynamically allocated array, but a pointer is a much more general data structure (see also question 4.1).
Due to the so-called equivalence of arrays and pointers (see question 6.3), arrays and pointers often seem interchangeable, and in particular a pointer to a block of memory assigned by malloc is frequently treated (and can be referenced using []) exactly as if it were a true array. See questions 6.14 and 6.16. (Be careful with sizeof; see question 7.28.)
See also questions 1.32, 6.10, and 20.14.
离线那个谁

只看该作者 沙发  发表于: 2013-07-10
问题:从实际情况来说,数组和指针的区别是什么?
回答:一个数组就是一块被预先分配好的连续的用来装元素(相同的类型)的位置。一个指针就是对任何位置的元素(一种特定的类型)的引用。一个指针必须指向被分配的空间的一个地方,但是它也可以被重新赋值(并且如果这个空间来自于malloc函数,那么可以被重新调整大小)。一个指针可以指向一个数组,可以(和malloc函数一起使用)模拟一个被指定的动态的数组,但是一个指针是一个更加通用的数据结构(参考4.1)。
由于所谓的数组和指针是等价的说法,数组和指针经常被看做是可互换的,特别是在指针指向一块被malloc函数分配的区域的时候往往被当做是一个真的数组(可以使用[]来引用)。参考问题6.14和6.16(使用sizeof时要注意了,参考问题7.28)。
参考问题1.32、6.10和20.14。
快速回复
限100 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
上一个 下一个