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

Question 6.2:But I heard that char a[] was identical to char *a.

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-8 16:58:26 | 显示全部楼层 |阅读模式
Q: But I heard that char a[] was identical to char *a.

A: Not at all. (What you heard has to do with formal parameters to functions; see question 6.4.) Arrays are not pointers, though they are
closely related (see question 6.3) and can be used similarly (see questions 4.1, 6.8, 6.10, and 6.14).

The array declaration char a[6] requests that space for six characters be set aside, to be known by the name ``a''. That is, there is a location named ``a'' at which six characters can sit. The pointer declaration char *p, on the other hand, requests a place which holds a pointer, to be known by the name ``p''. This pointer can point almost anywhere: to any char, or to any contiguous array of chars, or nowhere [footnote] (see also questions 5.1 and 1.30).

As usual, a picture is worth a thousand words. The declarations
                    char a[] = "hello";
                    char *p = "world";
would initialize data structures which could be represented like this:



It is useful to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location ``a'', move three past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location ``p'', fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In other words, a[3] is three places past (the start of) the object named a, while p[3] is three places past the object pointed to by p.
In the example above, both a[3] and p[3] happen to be the character 'l', but the compiler gets there differently. (The essential difference is that the values of an array like a and a pointer like p are computed differently whenever they appear in expressions, whether or not they are being subscripted, as explained further in question 6.3.) See also question 1.32.

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-8 22:02:58 | 显示全部楼层

问题:但是我听说char a[]和char *a是一样的。
回答:完全不是这样的(你所听说的情况是作为一个函数的形式参数的时候,参考问题6.4)。数组并不是指针,尽管他们密切相关(参考问题6.3)并且有类似的用处(参考问题4.1、6.8、6.10和6.14)。
数组char a[6]的声明会请求为其预留6个字符的位置,并且名字为"a"。因此,在名字为"a"处的位置可以存放6个字符。另一方面,对于char *p的声明,会请求一个位置用来存放一个指针,并且名字为"p"。这个指针几乎可以指向任何地方:任意字符或者任意连续的字符数组,也可以不指向任何地方(参考问题5.1和1.30)。
图形比语言更有说服力。以下声明:
char a[] = "hello";
    char *p = "world";
其初始化的数据结构可以像这样描述:


了解我们引用的像x[3]所产生的不同的代码是取决于这里的x是数组还是指针是非常有用的。考虑到上述的声明,当编译器看到表达式a[3]时,编译器就会在"a"开始的位置发出,然后向前移动3个字节,然后取出该字符。当编译器看到表达式p[3]时,编译器会在“p”位置处发出代码,然后取出那里的指针值,然后在指针上增加三,最后取出所指向的值。换句话说,a[3]就是从数组名开始的三个字节的位置。然而p[3]则是从p开始所指向的第三个内存处的值。在上面的例子中,a[3]和p[3]恰好都是字符'l',但是对于编译器来说,取到他们的方式是不同的。(本质的区别是数组a的值和指针p的值的计算方式不同,无论它们是出现在表达式中,或者是作为下标的情况。在问题6.3中有更清晰的描述),也可以参考问题1.32。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

Powered by 风叶林

© 2001-2026 Discuz! Team.

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