Difference between -> and . in a struct?
Asked Answered
G

8

33

If I have a struct like

struct account {
   int account_number;
};

Then what's the difference between doing

myAccount.account_number;

and

myAccount->account_number;

or isn't there a difference?

If there's no difference, why wouldn't you just use the . notation rather than ->? -> seems so messy.

Georgenegeorges answered 13/5, 2011 at 23:5 Comment(0)
D
64

-> is a shorthand for (*x).field, where x is a pointer to a variable of type struct account, and field is a field in the struct, such as account_number.

If you have a pointer to a struct, then saying

accountp->account_number;

is much more concise than

(*accountp).account_number;
Deicer answered 13/5, 2011 at 23:7 Comment(0)
T
29

You use . when you're dealing with variables. You use -> when you are dealing with pointers.

For example:

struct account {
   int account_number;
};

Declare a new variable of type struct account:

struct account s;
...
// initializing the variable
s.account_number = 1;

Declare a as a pointer to struct account:

struct account *a;
...
// initializing the variable
a = &some_account;  // point the pointer to some_account
a->account_number = 1; // modifying the value of account_number

Using a->account_number = 1; is an alternate syntax for (*a).account_number = 1;

I hope this helps.

Tapioca answered 13/5, 2011 at 23:22 Comment(0)
O
10

You use the different notation according to whether the left-hand side is a object or a pointer.

// correct:
struct account myAccount;
myAccount.account_number;

// also correct:
struct account* pMyAccount;
pMyAccount->account_number;

// also, also correct
(*pMyAccount).account_number;

// incorrect:
myAccount->account_number;
pMyAccount.account_number;
Outgoings answered 13/5, 2011 at 23:8 Comment(0)
O
5

-> is a pointer dereference and . accessor combined

Orcein answered 13/5, 2011 at 23:7 Comment(0)
S
4

If myAccount is a pointer, use this syntax:

myAccount->account_number;

If it's not, use this one instead:

myAccount.account_number;
Salami answered 13/5, 2011 at 23:7 Comment(0)
E
1

yes you can use struct membrs both the ways...

one is with DOt:(" . ")

myAccount.account_number;

anotherone is:(" -> ")

(&myAccount)->account_number;
Elmer answered 14/7, 2018 at 7:38 Comment(1)
You don't need (& and ) when using ->.Alejandraalejandrina
G
1

printf("Book title: %s\n", book->subject); printf("Book code: %d\n", (*book).book_code);

Glen answered 8/10, 2019 at 8:10 Comment(1)
You should explain your answer.Stonecrop
O
0

Quote from K & R the second edition. :

"The parentheses are necessary in (*pp).x because the precedence of the structure member operator . is higher than *. The expression *pp.x means *(pp.x), which is illegal here because x is not a pointer.

Pointers to structures are so frequently used that an alternative notation is provided as a shorthand."

If pp is a pointer to a structure, then pp->member-of-structure refers to the particular member.

Omaromara answered 10/5, 2024 at 17:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.