Yes, -> and . are both for member access, and . is for objects and -> is for pointers.

That doesn't explain why that is there though. Kids, gather around!

The reason we have this confusing stuff and carry it around to this day is because of bearded guy called Dennis Ritchie. He wrote the C Reference Manual. And is the father of C. And UNIX. And he did other stuff as well.

What Dennis Ritchie made and called C is far away form what we call C nowadays (and thank god for that). The C Reference Manual, CRM, had quirky definition for structs and their members, like seriously quirky. A struct member defined a global offset for the translation unit it occurred in, for example, take the following struct;

Code:
struct foo
{
   int a;
   int b;
   int c;
};



c would afterwards be a globally defined offset of 8, because it was at the 8th byte offset of the struct foo. Similar, a would have a defined offset of 0. And now you could do fun things like this:

Code:
int x;
x.b = 24;



It did exactly what no sane person would expect it to do: Write 24 at offset 4 of the integer x. So _after_ the integer.

Only issue, it didn't work with addresses, so it didn't work on pointers. Dereferencing the pointer first didn't work either, because the reason it doesn't work with pointers is also the reason the expression (*bar) was illegal because it simply requires an lvalue operand.

The solution was the -> operator, which didn't care if it was an lvalue or an rvalue, treated everything as an address, applied the offset, dereferenced it and then wrote the value you wanted into it. Example:
Code:
1024->b = 5; // Writes 5 to address 1028

int x = 4;
x->c = 42; // Writes 42 into 12



Yes, that doesn't make any sense. But, remember, b and c were globally defined offsets, and -> treated everything as address.

And because that's batshit insane, when Ken Thompson joined Dennis and they both made K&R C, they threw away this nonsense altogether. Under K&R C, this global offset stuff was gone, and the -> operator became equivalent to (*x).foo

And that's why both are still there, the -> operator also dereferences the pointer, in addition to what the . operator does. And that's why Ken Thompson got mad pussy and Dennis Ritchie is known as that bearded guy who wrote the CRM.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com