1

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.

Community
  • 1
  • 1
Dima
  • 2,012
  • 2
  • 17
  • 23
  • 3
    Dupe, many times (http://stackoverflow.com/questions/221346/what-is-the-arrow-operator-synonym-for-in-c - Yes, it's a C++ question, but it's the same operator). – Chris Lutz Jan 26 '10 at 10:30

4 Answers4

8

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
  • 55,802
  • 15
  • 111
  • 149
  • 1
    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 Jan 26 '10 at 10:28
  • 1
    Actually, * dereferences a pointer. -> dereferences a pointer and then accesses a field of the struct it pointed to. – jrockway Jan 26 '10 at 10:29
  • 1
    `loader` could be an `union`. – ntd Jan 26 '10 at 12:36
8

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

chris
  • 664
  • 1
  • 12
  • 23
5

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 Singhal
  • 93,253
  • 21
  • 125
  • 158
3

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

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102