开发者

What does "->" stand for in C programming, Gtk+ programming? [duplicate]

开发者 https://www.devze.com 2022-12-17 22:45 出处:网络
This question already has answers here: 开发者_StackOverflow中文版 Closed 12 years ago. Possible Duplicate:
This question already has answers here: 开发者_StackOverflow中文版 Closed 12 years ago.

Possible Duplicate:

What is the arrow operator (->) synonym for in C++?

I couldn't find documentation on the "->" which is used a lot in Gnome codebase. For example in gedit they have this:

loader->document = g_value_get_object (value)

What is document in relation to loader? There are many other examples as well with more basic widgets as well.


loader is a pointer. -> dereferences a pointer to a struct. It's the same as typing (*loader).

Hence:

struct smth {
  int a;
  int b;
};

struct smth blah;
struct smth* pblah;

...to get access to a from blah, you need to type blah.a, from pblah you need to write pblah->a. Remember that it needs to point to something though!


loader->document is same as: (*loader).document


loader is a pointer to a struct or a union. The struct/union has at least one member, named document:

struct astruct {
    T document;
};

T above is the type of document, and is also the type returned by g_value_get_object().

Then, given the declarations below:

struct astruct s;
struct astruct *loader = &s;

the following are equivalent:

s.document = ...
loader->document = ...
(*loader).document = ...

Formally, -> is a binary operator, whose first operand has a type "pointer to a structure or pointer to union", and the second operand is the name of a member of such a type.


loader is a pointer to a struct that has a document field, -> is used to access it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号