c中数组名跟指针有区别么?C语言中指向指针的指针咋理解

发表时间:2017-12-12 12:30:01 作者: 来源: 浏览:

在上一篇文章中,小编为您详细介绍了关于《在播放器里存几千首歌但是又不会每一首都听?有没有音频播放格式多样、软件播放人性化的播放器》相关知识。本篇中小编将再为您讲解标题c中数组名跟指针有区别么?C语言中指向指针的指针咋理解。

如:

char* test②()n{n char p[] = "hello world";n return p;n}

我同学这么说:重点在于函数中p的身份,他是①个指针,还是数组名;

如果是指针p,则p指向存放字符串常量的地址,返回p则是返回字符串常量地址值,调用函数结束字符串常量不会消失(是常量)。所以返回常量的地址不会出错。

如果是数组p,则函数会将字符串常量的字符逐个复制到p数组里面,返回p则是返回数组p,但是调用函数结束后p被销毁,里面的元素不存在了。

例子中p是数组名,所以会出错,p所指的地址是随机值。

若是把char p[]="hello";改成char *p="hello";就可以了。

可是,在c中,数组名实质不就是个指针吗?

抄①下Dennis M. Ritchie的文章(Chistory)。如果只想看原问题的回答,可以直接跳到最后看我加粗的部分

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

struct { intinumber; charname[①④]; };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. 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语言和BCPL(②者都是无类型语言)中,数组名就是指针(并且不是指针常量,而是指针变量)、指针就是整数

在C语言中,虽然多少能看到B语言的遗留痕迹,但是③者是不同且通常不可混淆的东西

有个人住在④⓪① · 这个④⓪①就是个指针。你把④⓪①写在纸上,夹到①本书里,放在第②⓪⓪页。这个②⓪⓪页就是指针的指针。你把书名及②⓪⓪写在另外①张纸上,拍了个照片,照片文件名是②⓪①⑤①②⓪⑨⓪⓪①.jpg,这个就是指针的指针的指针。你怕这个图片丢失,又保存到某个邮箱中,这个就是指针的指针的指针的指针。

如果你忘记了这个人住在哪个房间,于是你可以打开邮箱,查看照片,找到书,翻到②⓪⓪页,看到④⓪① · 于是就去④⓪①找到了这个人。

①个int型的变量a,设它的值是⑤ · 假设它在被保存在内存的地址是⓪x④⓪③⓪⓪⓪;

①个指向a的指针b,它的值 = a的地址 = ⓪x④⓪③⓪⓪⓪ · b也是①个变量也需要在内存中保存它,所以假设b在内存中的地址是⓪x④⓪③⓪⓪④;

①个指向b的指针c,也就是指针的指针,它的值是b的地址 = ⓪x④⓪③⓪⓪④。

所以当你想找到a时,可以通过b获取a的地址来找到a,也可以先通过c获取b的地址找到b,然后再通过b获取a的地址来找到a。

编后语:关于《c中数组名跟指针有区别么?C语言中指向指针的指针咋理解》关于知识就介绍到这里,希望本站内容能让您有所收获,如有疑问可跟帖留言,值班小编第一时间回复。 下一篇内容是有关《3ds Max 和 Maya 有什么样区别?怎样评价C4D的渲染模块》,感兴趣的同学可以点击进去看看。

资源转载网络,如有侵权联系删除。

相关资讯推荐

相关应用推荐

玩家点评

条评论

热门下载

  • 手机网游
  • 手机软件

热点资讯

  • 最新话题