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

Question6.12:Since array references decay into pointers,what's the difference between them?

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-19 20:35:24 | 显示全部楼层 |阅读模式
Q: Since array references decay into pointers, if arr is an array, what's the difference between arr and &arr?
A: The type.
In Standard C, &arr yields a pointer, of type pointer-to-array-of-T, to the entire array. (In pre-ANSI C, the & in &arr generally elicited a warning, and was generally ignored.)Under all C compilers, a simple reference (without an explicit &) to an array yields a pointer, of type pointer-to-T, to the array's first
element.
For a simple array
                          int a[10];
a reference to a has type ``pointer to int,'' and &a is ``pointer to array of 10 ints.'' For a two-dimensional array like
                           int array[NROWS][NCOLUMNS];
a reference to array has type ``pointer to array of NCOLUMNS ints,'' while &array has type ``pointer to array of NROWS arrays of NCOLUMNS ints.''
See also questions 6.3, 6.13, and 6.18.

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-19 21:22:39 | 显示全部楼层
问题:由于数组引用退化为指针,如果arr是一个数组,那么arr和&arr之间的区别是什么?
回答:区别在于类型。
在标准C中,&arr会产生一个T型数组指针指向整个数组(在ANSI C之前,&arr中的&通常会引出警告,并且通常是被忽略的)。在所有的C编译器中,一个数组的简单的引用(没有&)会产生一个T型指针指向数组的第一个元素。
对于一个简单数组
         int a[10];
一个对于a的引用的类型是“int型数组的指针”,&a则是“10个int数组的指针”。对于二维数组,如
         int array[NROWS][NCOLUMNS];
对array的引用的类型是“NCOLUMNS”个int的数组的指针,而array的类型则是“NROWS个NCOLUMN个int的数组的指针”。
参考问题6.3、6.13和6.18。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

GMT+8, 2026-2-2 04:19 , Processed in 0.063760 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

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