找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 276|回复: 1

Question 6.8:Pratically speaking,what is the difference between arrays and pointers?

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-10 20:39:37 | 显示全部楼层 |阅读模式

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.

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-10 21:05:11 | 显示全部楼层
问题:从实际情况来说,数组和指针的区别是什么?
回答:一个数组就是一块被预先分配好的连续的用来装元素(相同的类型)的位置。一个指针就是对任何位置的元素(一种特定的类型)的引用。一个指针必须指向被分配的空间的一个地方,但是它也可以被重新赋值(并且如果这个空间来自于malloc函数,那么可以被重新调整大小)。一个指针可以指向一个数组,可以(和malloc函数一起使用)模拟一个被指定的动态的数组,但是一个指针是一个更加通用的数据结构(参考4.1)。
由于所谓的数组和指针是等价的说法,数组和指针经常被看做是可互换的,特别是在指针指向一块被malloc函数分配的区域的时候往往被当做是一个真的数组(可以使用[]来引用)。参考问题6.14和6.16(使用sizeof时要注意了,参考问题7.28)。
参考问题1.32、6.10和20.14。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 04:40 , Processed in 0.159172 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表