免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: mickgrady
打印 上一主题 下一主题

二维数组指针,编译器到底为我们处理了多少 [复制链接]

论坛徽章:
0
41 [报告]
发表于 2011-03-16 21:31 |只看该作者
这是标准写的, 确实大家对左值理解都不一样,所以讨论也没什么对错可言了

An lvalue is an expression with an object type or an incomplete type other than void;
  if an lvalue does not designate an object when it is evaluated, the behavior is undefined.
  When an object is said to have a particular type, the type is specified by the lvalue used to   designate the object. A modifiable lvalue is an lvalue that does not have array type, does   not have an incomplete type, does not have a const-qualified type, and if it is a structure  or union, does not have any member (including, recursively, any member or element of  all contained aggregates or unions) with a const-qualified type.


Except when it is the operand of the sizeof operator, the unary & operator, the ++
  operator, the -- operator, or the left operand of the . operator or an assignment operator,
  an lvalue that does not have array type is converted to the value stored in the designated
   The cast and assignment operators are still required to perform their specified conversions as
       described in 6.3.1.4 and 6.3.1.5.



The name ‘‘lvalue’’ comes originally ---------------------------

from the assignment expression E1 = E2, in which the left
       operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an
       object ‘‘locator value’’. What is sometimes called ‘‘rvalue’’ is in this International Standard described
       as the ‘‘value of an expression’’.
       An obvious example of an lvalue is an identifier of an object. As a further example, if E is a unary
       expression that is a pointer to an object, *E is an lvalue that designates the object to which E points.
  
  object (and is no longer an lvalue). If the lvalue has qualified type, the value has the
  unqualified version of the type of the lvalue; otherwise, the value has the type of the
  lvalue. If the lvalue has an incomplete type and does not have array type, the behavior is
  undefined.


Except when it is the operand of the sizeof operator or the unary & operator, or is a
  string literal used to initialize an array, an expression that has type ‘‘array of type’’ is
  converted to an expression with type ‘‘pointer to type’’ that points to the initial element of
  the array object and is ___not ____ an lvalue.

这段话说 T t[N], t的类型大多数时候是 T*, 而且它不是左值, 同样int a[2][3]; *a hash type of "array of int" , so converted to int *, and is not an lvalue
K&R也是这个意思, 这也是我上面帖子的来源参考
不管怎么理解左值,标准这一段(上面几段我不知所言)确实说int a[2][3]; *a不是左值

C语言参考手册基本就是抄标准的吧,我这里也有,没怎么看过,很像
KBTiller 写的不错,也和标准也很像

论坛徽章:
0
42 [报告]
发表于 2011-03-16 21:41 |只看该作者
KBTILLER说
T  t[N];
sizeof(t)中的t是左值, 我不太好理解,上面这最后的Except是在说t 在sizeof中不是T*还是说t是左值
我觉得是说强调前者吧
不过t在这里都不是左值的话那哪还能是呢

标准只是说
It is __perhaps__ better ___considered as___ representing an    object ‘‘locator value’’.
就像supermegaboy 说的那样
这几个词根本说不上严格吧

即使是第一句,貌似也只是在解释左值像什么,而不是定义

论坛徽章:
0
43 [报告]
发表于 2011-03-16 22:42 |只看该作者
回复 41# flw2


    Except when it is the operand of the sizeof operator or the unary & operator, or is a
  string literal used to initialize an array, an expression that has type ‘‘array of type’’ is
  converted to an expression with type ‘‘pointer to type’’ that points to the initial element of
  the array object and is ___not ____ an lvalue.

这段话说 T t[N], t的类型大多数时候是 T*, 而且它不是左值, 同样int a[2][3]; *a hash type of "array of int" , so converted to int *, and is not an lvalue
K&R也是这个意思, 这也是我上面帖子的来源参考
不管怎么理解左值,标准这一段(上面几段我不知所言)确实说int a[2][3]; *a不是左值
------------------------------------------------------------------------------------------------------------------------
我真是倒了,你看这段话竟然看出这样一个结果出来?

“这段话说 T t[N], t的类型大多数时候是 T*, 而且它不是左值”?

你还需要重新深入思考。我现在都有点觉得,我前面几个帖子是否对你期望太高了?不过这不要紧,理解需要个过程,六七年前我也一度以为数组名是一个右值。

知道这段话是什么东西吗??这就是被大家平时说烂了的数组到指针的隐式转换,它阐述的是在表达式中数组类型如何转换到一个右值指针,注意,是“转换到”!!

它并非阐述数组类型的表达式是什么,而是说这是一个转换。

你写的这段话:“这段话说 T t[N], t的类型大多数时候是 T*, 而且它不是左值”,其实就是“数组名是一个指针”这种错误的来源!

论坛徽章:
0
44 [报告]
发表于 2011-03-16 22:49 |只看该作者
“这段话说 T t[N], t的类型大多数时候是 T*, 而且它不是左值”,
--------------------------------------------------------------------------
撇开原理不说,就以这句话来说,你不觉得已经是自相矛盾了么?

T t[N]已经说明t的类型是T[N],我想这不需要再说为什么了吧?T t[N]就是t的定义,但又说t的类型是T*,你不觉得存在大大的矛盾么?

我想你现在已经开始糊涂了,究竟数组名是数组类型还是指针类型已经搞不清了,没关系,给自己时间,然后,think it over!

论坛徽章:
0
45 [报告]
发表于 2011-03-16 22:51 |只看该作者
现在变成了左值和右值之争了~
我在网上搜到lvalue是location value的意识不是left value,而rvalue是read value非right value,看你怎么理解咯。

论坛徽章:
0
46 [报告]
发表于 2011-03-16 23:04 |只看该作者
本帖最后由 KBTiller 于 2011-03-16 23:05 编辑
现在变成了左值和右值之争了~
我在网上搜到lvalue是location value的意识不是left value,而rvalue是read  ...
liexusong 发表于 2011-03-16 22:51


location value 、read  value的说法确实更好些
但也确实不是最初的术语

论坛徽章:
0
47 [报告]
发表于 2011-03-17 09:53 |只看该作者
回复  flw2


    Except when it is the operand of the sizeof operator or the unary & operator, o ...
supermegaboy 发表于 2011-03-16 22:42


你不用考虑我是否明白隐式转换,就这个问题议论就可以了
说实话,我很怕玩弄文字
数组名到指针的转换我楼上其实也说了不少吧,你没看到而已
43楼我觉得你说的内容对讨论没有什么价值,所以我就不回复了


------------------------------------------------------------------------------
“这段话说 T t[N], t的类型大多数时候是 T*, 而且它不是左值”,
--------------------------------------------------------------------------
撇开原理不说,就以这句话来说,你不觉得已经是自相矛盾了么?

T t[N]已经说明t的类型是T[N],我想这不需要再说为什么了吧?T t[N]就是t的定义,但又说t的类型是T*,你不觉得存在大大的矛盾么?

我想你现在已经开始糊涂了,究竟数组名是数组类型还是指针类型已经搞不清了,没关系,给自己时间,然后,think it over!




你别抠字眼了,
n expression that has type ‘‘array of type’’ is
  converted to an expression with type ‘‘pointer to type’’ that points to the initial element of
  the array object
我说”是“只是在说它被转换,因为这您就认为我不懂数组到指针的转换,你的水平看的出来不一般,你这么认为我一点都不介意
关键是你也没做有益于讨论的解释


对于你说的
"它并非阐述数组类型的表达式是什么,而是说这是一个转换。"
大家都理解转换,如果说它并非是想说数组类型的表达式是什么(我还是认为它在解释数组类型的表达式是什么,转化。。等等)
另外,我再一字不差抄一段原话(C语言参考手册)

左值是引用对象的表达式,可以检查或改变对象。
不能成为左值的表达式: 数组名,函数,枚举常量,赋值,类型转换和函数调用。

但是你说了int a[2][3]; *a不是左值
int a[2];  a你应该也认为不左值吧(你没说),你认为是我也不觉得你不理解int a[2],相反你很理解。
但是书上确实没有转弯地说a不是左值。


我一直是觉得标准本身只是对左值的解释,包括K&R,都只是在解释,说实话我没怎么看过标准
你能用一句话告诉我什么是左值吗? 用中文。

论坛徽章:
0
48 [报告]
发表于 2011-03-17 10:06 |只看该作者
如果说K&R过时了,理解,但是你别说C语言参考手册说的此左值非你理解的左值
搬它出来没有别的意思,只是怕我解释不好,并不是尽信它的意思。
一个连定义是什么都不清楚的讨论一点意义都没有,这种问题也没有必要再讨论了
令: 我很佩服你的水平,六七年前我手头只有教授的书

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
49 [报告]
发表于 2011-03-17 10:52 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
50 [报告]
发表于 2011-03-17 13:20 |只看该作者
回复 1# mickgrady


    级别是不一样的,二维数组的级别为2级所以a为2级,*a为一级,**a为0级,也就是a【0】【0】!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP