What is the difference between .
and ->
when accessing data in a C structure? I couldn't find any difference in my several attempts. Both provide me access to the desired data.

- 3,119
- 19
- 19
- 37

- 400
- 1
- 6
- 20
-
-> use when you try to call smt from pointer to struct . when you create object and call – Ivan Sheihets Jun 21 '17 at 11:38
-
If you can't find any difference, try replacing one with the other without changing anything else, and see if your program still compiles :-) – Sergey Kalinichenko Jun 21 '17 at 11:39
-
4There is literally no code where those two things have no difference. One will always fail to compile – M.M Jun 21 '17 at 11:39
-
1What does your data look like? What does your struct look like? Could you give a minimal example? – Theolodis Jun 21 '17 at 11:39
-
What does the text book tell about these operators? – Gerhardh Jun 21 '17 at 14:26
5 Answers
The ->
operator is only syntactical sugar:
x->y
is the same as
(*x).y
The parentheses are necessary due to the .
operator having higher precedence than the *
operator.

- 18,090
- 24
- 36
- 51
6.5.2.3 Structure and union members
Constraints
1 The first operand of the . operator shall have an atomic, qualified, or unqualified structure or union type, and the second operand shall name a member of that type.
2 The first operand of the -> operator shall have type ‘‘pointer to atomic, qualified, or unqualified structure’’ or ‘‘pointer to atomic, qualified, or unqualified union’’, and the second operand shall name a member of the type pointed to.
Semantics
3 A postfix expression followed by the . operator and an identifier designates a member of a structure or union object. The value is that of the named member,95) and is an lvalue if the first expression is an lvalue. If the first expression has qualified type, the result has the so-qualified version of the type of the designated member.
4 A postfix expression followed by the -> operator and an identifier designates a member of a structure or union object. The value is that of the named member of the object to which the first expression points, and is an lvalue.96) If the first expression is a pointer to a qualified type, the result has the so-qualified version of the type of the designated member.

- 15,386
- 4
- 57
- 74
struct MyStruct
{
int a;
}
MyStruct *st;
st->a = 10;
MyStruct st2;
st.a = 10;

- 802
- 8
- 17
There is no connection between ->
operator and plain structs / unions
or any other type, other than pointers to structs / unions
. The ->
is accessing a member in struct / union
pointed by your pointer. Meaning, after creating a struct / union
with members, the struct / union
members can be accessed either by .
if holding the struct / union
itself or by ->
if holding the pointer
to the struct / union
An Example:
// creating one instance of struct s, and a pointer to an instance of struct s. struct s is a struct holding one int called 'data'.
struct s{int data;}struct_s_instance, *struct_s_instance_pointer = malloc(sizeof(struct s));
struct_s_instance.data = 3; // access using the '.' operator
struct_s_instance_pointer->data = 4; // pointer access using the '->' operator
printf("%d-%d", struct_s_instance.data, struct_s_instance_pointer->data);
You can't access data
in struct_s_instance_pointer
using the .
(i.e. struct_s_instance_pointer.data
) or access data
in struct_s_instance
using ->
(i.e. struct_s_instance->data
). These are totaly different things.
Note that when given a pointer, such as struct_s_instance_pointer
you can derefernce it: *struct_s_instance_pointer
and then the operator .
can and should be used: (*struct_s_instance_pointer).data

- 8,529
- 8
- 37
- 63

- 12,097
- 11
- 59
- 124
-
"*There is no connection between -> operator and structs, unions*" -- sure there is – Jun 21 '17 at 11:54
-
@FelixPalmen how can you access data in a struct (not a pointer) using -_>_? – CIsForCookies Jun 21 '17 at 11:56
-
How can you access an object that *isn't* a struct or union member using `->`? – Jun 21 '17 at 11:57
-
I guess I'm missing something but -> is used only for pointers. Meaning, no usage for structs, unions (and also ints, chars and such). Am I wrong? – CIsForCookies Jun 21 '17 at 11:59
-
1`->` is shorthand for dereference and member access. So sure it requires a pointer. It *also* requires the pointer to point to either a `struct` or a `union`, because only in structs and unions, you can access *members*. – Jun 21 '17 at 12:03
-
You are totally right. I meant they have no connection to plain structs or unions without pointers. I will edit – CIsForCookies Jun 21 '17 at 12:07