-36

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.

user16217248
  • 3,119
  • 19
  • 19
  • 37
M.Ferru
  • 400
  • 1
  • 6
  • 20

5 Answers5

6

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.

Ctx
  • 18,090
  • 24
  • 36
  • 51
3

. is used with structs. -> is used with pointers (to structs).

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

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.

alinsoar
  • 15,386
  • 4
  • 57
  • 74
1
struct MyStruct
{
  int a;
}

MyStruct *st;
st->a = 10;

MyStruct st2;
st.a = 10;
Ivan Sheihets
  • 802
  • 8
  • 17
0

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

Alec
  • 8,529
  • 8
  • 37
  • 63
CIsForCookies
  • 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