为什么C之间有区别 - >和? [英] Why does C have a distinction between -> and .?

查看:104
本文介绍了为什么C之间有区别 - >和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,这是没有严重的后果,但它被窃听我
同时:是否有区分的一个原因 - > 运营商

OK, this is of no serious consequence, but it's been bugging me for a while: Is there a reason for the distinction between the -> and . operators?

当然,目前的规则是, 作用于结构和 - 方式> 作用于
一个指针到结构(或联合)。但在这里它是如何工作在实践中。
取值是一个结构incuding元素 X ,并让 PS 是指向同一形式的结构。

Of course, the current rule is that . acts on a struct, and -> acts on a pointer-to-struct (or union). But here's how it works in practice. Let s be a struct incuding an element x, and let ps be a pointer to a struct of the same form.

如果你写

s->x

,编译器会吐出在路上

the compiler will spit out a warning in the way of

您的意思s.x.请重新键入和重新编译。

You meant s.x. Please retype that and recompile.

如果你写

ps.x

,编译器会吐出在路上

the compiler will spit out a warning in the way of

您的意思PS-系列> X。请重新键入和重新编译。

You meant ps->x. Please retype that and recompile.

由于编译器知道这两种类型取值 PS 在编译的时候,它拥有所有的信息,则需要跨preT怎样才是正确的操作员会。我怀疑这是不是像其它警告(如缺少分号),在没有对正确的修复歧义。

Because the compiler knows the type of both s and ps at compile time, it has all the information it needs to interpret what the correct operator would be. I suspect that this isn't like other warnings (like a missing semicolon), in that there is no ambiguity about the correct fix.

所以这里有一个假设的提案,C1X标准委员会(即永远不会被考虑,因为ISO是一个保守的条纹):

So here's a hypothetical proposal to the C1x standards committee (that would never be considered, because the ISO is on a conservative streak):

由于前pression lhs.rhs,如果LHS是一个结构或联合类型,
      那么前pression应参照命名RHS LHS的元素。
      如果LHS的类型指针到结构或-union的,那么这应是
      除preTED为(*左).rhs。

Given the expression lhs.rhs, if lhs is a struct or union type, then the expression shall refer to the element of lhs named rhs. If lhs is of type pointer-to-struct or -union, then this shall be interpreted as (*lhs).rhs.

这肯定会节省大家的时间,使人们更容易学习C [和我教过够下与学习者找到权威说 - > 的事情是不是混淆或烦人。]

This would certainly save us all time, and make it easier for people to learn C [and I've taught enough C to say with authority that learners find the -> thing to be either confusing or annoying.]

甚至还有precedent,其中C做类似的事情了一把。例如,对于实现的原因,函数声明总是转换为指针到功能,使 F(X,Y)(* F)( X,Y)都将工作无论 F的被宣布为一个函数或函数指针。

There's even precedent, where C does a handful of similar things. E.g., for implementation reasons, function declarations are always cast to pointer-to-function, so f(x,y) and (*f)(x,y) will both work regardless of whether f was declared as a function or a pointer to function.

所以,我的问题:什么是错的这个建议?你能想到的例子,就不会有 ps.x 之间致命的模糊性和 SX ,为什么还是保持强制性的区别否则有用吗?

So, my question: what's wrong with this proposal? Can you think of examples where there would be fatal ambiguity between ps.x and s.x, or why keeping the mandatory distinction is otherwise useful?

推荐答案

好吧,如果你真的想引进的那种功能集成到C语言的规范,那么,以使其混合与的其余语言逻辑的事情将是延长衰减到指针结构类型的概念。你自己一个函数和函数指针做出了表率。它的工作原理是这样的原因是因为在C函数类型衰变为指针类型在所有情况下,除了的sizeof 和一元&安培; 运营商。 (同样的事情发生在数组,BTW)。

Well, if you really wanted to introduce that kind of functionality into the specification of C language, then in order to make it "blend" with the rest of the language the logical thing to do would be to extend the concept of "decay to pointer" to struct types. You yourself made an example with a function and a function pointer. The reason it works that way is because function type in C decays to pointer type in all contexts, except for sizeof and unary & operators. (The same thing happens to arrays, BTW.)

因此​​,为了实现类似于你有什么建议,我们可以引入结构到指针衰减这一概念的东西,这将在完全相同的方式工作,与所有其他在C衰减(即,数组到指针的衰变和功能到指针衰减)工作:当类型的结构对象 T 在一个前pression使用,它的类型立即衰减键入 T * - 指向结构对象的开始 - 除非它是中的sizeof 操作数或一元&安培; 。一旦这种腐烂规则引入了结构,你可以使用 - > 操作员访问结构元素,无论你是否有一个指针结构或左边的结构本身 - 手的一面。 。操作 将成为这种情况下,完全没有必要的(除非我失去了一些东西),你总是用 - > 只有 - 方式>

So, in order to implement something similar to what you suggest, we could introduce the concept of "struct-to-pointer decay", which would work in exactly the same way as all other "decays" in C (namely, array-to-pointer decay and function-to-pointer decay) work: when a struct object of type T is used in an expression, its type immediately decays to type T* - pointer to the beginning of the struct object - except when it's an operand of sizeof or unary &. Once such a decay rule is introduced for structs, you could use -> operator to access struct elements regardless of whether you have a pointer to struct or the struct itself on the left-hand side. Operator . would become completely unnecessary in this case (unless I'm missing something), you'd always use -> and only ->.

以上,再一次,这个功能是什么样子,在我看来,如果它是在C语言的精神执行。

The above, once again, what this feature would look like, in my opinion, if it was implemented in the spirit of C language.

不过,我会说(用什么查尔斯说同意)表示,code与指针的作品之间的视觉区别的损失,结构和code,与结构本身不完全是不可取的。作品

But I'd say (agreeing with what Charles said) that the loss of visual distinction between the code that works with pointers to structs and the code that works with structs themselves is not exactly desirable.

P.S。这样的衰减规律,为结构的一个明显的负面后果是,除了新手无私地相信数组只是不断的指针当前军队,我们就会有新手无私地相信结构对象都只是不断的指针的军队。和克里斯托雷克的阵列常见问题必须是约1.5-2x到盖结构较大,以及:)

P.S. An obvious negative consequence of such a decay rule for structs would be that besides the current army of newbies selflessly believing that "arrays are just constant pointers", we'd have an army of newbies selflessly believing that "struct objects are just constant pointers". And Chris Torek's array FAQ would have to be about 1.5-2x larger to cover structs as well :)

这篇关于为什么C之间有区别 - >和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆