tags:

views:

147

answers:

4

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.

+7  A: 

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!

Kornel Kisielewicz
Specifically, it is a pointer to a `struct`, and `->` accesses a member of the `struct` that it points to. It is equivalent to `(*loader).document` except for being shorter and clearer.
Chris Lutz
Actually, * dereferences a pointer. -> dereferences a pointer and then accesses a field of the struct it pointed to.
jrockway
Happy anonymous downvoting?
Kornel Kisielewicz
`loader` could be an `union`.
ntd
+3  A: 

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

ammoQ
+8  A: 

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

chris
+5  A: 

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.

Alok