C语言数组和指针的区别

一、定义

  • 指针:表示C语言中某种数据类型的数据存储的内存地址,例如,指向各种整型的指针或者指向某个结构体的指针;
  • 数组:表示若干个相同C语言数据类型的元素在连续内存中储存的一种形态。

二、区别

数组和指针本质上都代表一块内存,数组名即代表这块内存的地址,而指针本身不代表任何有意义的内容,只有给它赋值后,它才真正的表示一块有意义的内存地址。
数组在编译时就已经被确定下来,而指针直到运行时才能被真正的确定到底指向何方。
指针需要经过编译、链接、运行时等重重考验才能获得属于自己的内存。数组的这些内存一旦确定下来就不能轻易的改变了,这些内存会伴随数组一生;而指针则有很多的选择,在其一生他可以选择不同的内存地址,比如一个字符指针可以指向单个字符同时也可代表多个字符等。
由于数组名直接代表其地址,而指针需要通过读取其保存的地址才能读取相应数据,所以数组为直接方式,而指针为间接访问。
在编写程序时,对于数组访问应该写成指针的形式,因为这样可以提高效率。对于现代的编译器而言,这个说法一般是错误的!现代编译器对于数组的访问都会自动优化为其对应的指针加偏移量的形式,所以也就没有哪种形式效率更高的说法了。实际上,数组的访问之所以改写为指针加偏移量的方式,是因为其为系统底层最基本的工作方式。
在编写程序时,对于数组访问应该写成指针的形式,因为这样可以提高效率。对于现代的编译器而言,这个说法一般是错误的!现代编译器对于数组的访问都会自动优化为其对应的指针加偏移量的形式,所以也就没有哪种形式效率更高的说法了。实际上,数组的访问之所以改写为指针加偏移量的方式,是因为其为系统底层最基本的工作方式。

Problems became evident when I tried to extend the type notation, especially to add structured (record) types. Structures, it seemed, should map in an intuitive way onto memory in the machine, but in a structure containing an array, there was no good place to stash the pointer containing the base of the array, nor any convenient way to arrange that it be initialized. For example, the directory entries of early Unix systems might be described in C as
当我尝试扩展类型符号,尤其是添加结构(记录)类型时,问题变得很明显。结构似乎应该以直观的方式映射到机器的内存中,但是在包含数组的结构中,没有好地方来存放包含数组基数的指针,也没有任何方便的方法来安排它初始化。例如,早期 Unix 系统的目录条目可能在 C 中描述为

  struct {
    	int	inumber;
    	char	name[14];
    };

我希望该结构不仅可以表征抽象对象,还可以描述可能从目录中读取的位集合。编译器可以在哪里取命名语义要求的指针?即使结构被认为更抽象,并且指针的空间可以以某种方式隐藏,我如何处理在分配复杂对象时正确初始化这些指针的技术问题,也许是一个指定结构包含任意深度结构的数组的结构?
I wanted the structure not merely to characterize an abstract object but also to describe a collection of bits that might be read from a directory. Where could the compiler hide the pointer to name that the semantics demanded? Even if structures were thought of more abstractly, and the space for pointers could be hidden somehow, how could I handle the technical problem of properly initializing these pointers when allocating a complicated object, perhaps one that specified structures containing arrays containing structures to arbitrary depth?

The solution constituted the crucial jump in the evolutionary chain between typeless BCPL and typed C. It eliminated the materialization of the pointer in storage, and instead caused the creation of the pointer when the array name is mentioned in an expression. The rule, which survives in today’s C, is that values of array type are converted, when they appear in expressions, into pointers to the first of the objects making up the array.
该解决方案构成了无类型 BCPL(Basic Combined Programming Language) 和有类型 C 之间进化链中的关键跳跃。它消除了指针在存储中的物化,而是在表达式中提到数组名时导致创建指针。在今天的 C 语言中仍然存在的规则是,当数组类型的值出现在表达式中时,它们会被转换为指向组成数组的第一个对象的指针。
This invention enabled most existing B code to continue to work, despite the underlying shift in the language’s semantics. The few programs that assigned new values to an array name to adjust its origin—possible in B and BCPL, meaningless in C—were easily repaired. More important, the new language retained a coherent and workable (if unusual) explanation of the semantics of arrays, while opening the way to a more comprehensive type structure.
这项发明使大多数现有的 B 代码能够继续工作,尽管语言的语义发生了潜在的变化。为数组名称分配新值以调整其来源的少数程序(在 B 和 BCPL 中可能,在 C 中无意义)很容易修复。更重要的是,新语言保留了对数组语义的连贯且可行(如果不寻常)的解释,同时为更全面的类型结构开辟了道路。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值