user1756845
user1756845

Reputation: 19

C syntax understanding issue

I have a piece of code which says

pointer = &arbitarystruct

pointer->x1
*pointer->x2

Is it the same expression or do x1/x2 belong to two different structs.

Upvotes: -1

Views: 83

Answers (2)

hobbs
hobbs

Reputation: 239881

*pointer->x2 is *(pointer->x2) — that is, it's dereferencing pointer->x2, which should itself be a pointer. It doesn't involve *pointer at all. If it did, it would be invalid, as (*pointer)->x2 is invalid.

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263257

Apparently arbitrarystruct is a structure that contains two members named x1 and x2.

pointer->x1 refers to the x1 member of arbitrarystruct (accessed indirectly via the pointer).

pointer->x2 refers to the x2 member of that same struct object.

Apparently the x2 member is a pointer. *pointer->x2, which is equivalent to *(pointer->x2) (-> binds more tightly than *) deferences that pointer.

Upvotes: 1

Related Questions