C 中的这个运算符(->)是什么?
可能的重复:
C 中箭头运算符 (->) 的用法
据我所知,只有 C++ 可以使用类(obj->something) 但是我在许多 C 应用程序中看到过这个运算符。
还有一个小问题。通常人们在 C 中使用这样的结构:
structname.somevariable
但是我见过它们这样使用:
structname.something1.something2
它与关键字 union 有关系吗?
Possible Duplicate:
Arrow operator (->) usage in C
As far as i know only C++ can use classes(obj->something) however i have seen this operator in numerous C applications.
And a small side-question. Usually one uses structures in C like this:
structname.somevariable
However i have seen them used like:
structname.something1.something2
Does it have something to do with the keyword union?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,这与工会无关。这只是获得会员的会员。
And no, that has nothing to do with unions. It's just getting a member of a member.
如果你有一个指向结构体对象的指针,就像
使用
->
访问成员一样,如果 p 不是指针,则使用
访问成员。
任何 C 书籍都涵盖了这一点:P
if you have a pointer to a struct object, like
you access members with
->
if p is not a pointer, you access members with
.
Any C book covers this :P
运算符
->
用于通过指向该结构的指针来访问该结构的成员。您问题的第二部分在访问嵌套结构时使用,它不限于使用联合。例如:Operator
->
is used to access a member of astruct
through a pointer to that structure. The second part of your question is used when accessing nested structures, it is not restricted to the use of unions. For instance:C++ 类是 C 结构体的扩展,对象只是指向结构体的指针。 C++ 实际上并没有发明很多新东西;而是它被称为 C ++ 是有原因的。
结构体可以嵌套。鉴于
您可以参考
a.b_b.a_b
。union
使用与struct
类似的语法,但所有成员都相互重叠。当您需要以多种方式解释一块内存时,这非常有用。我强烈建议拿起一本 C 书。
C++
class
es are an extension of Cstruct
s, and an object is just a pointer to astruct
. C++ didn't actually invent a whole lot of new stuff; it's called C ++ for a reason.struct
s can be nested. Givenyou can refer to
a.b_b.a_b
.A
union
uses similar syntax to astruct
, but all the members overlap each other. This is useful when you need to interpret a chunk of memory in multiple ways.I strongly suggest picking up a C book.
当您想要访问一个结构体的成员,而该结构体本身又是另一个结构体的成员时,可以使用 xyz。
x.y.z is used when you want to access a member of a struct that is itself a member of another struct.
我用 C 写了一个小例子。
I've written a small example in C.
如果我没记错的话,在 C++ 中
完全 与 相同
,并且
完全 与
If I remember correctly, in C++
is exactly the same as
and
is exactly the same as