R
R
rsatarov2017-10-19 19:17:00
C++ / C#
rsatarov, 2017-10-19 19:17:00

When should you use pointers to struct members?

In what cases is it necessary to address through a dot, and in which cases through an arrow?
Let's say this function:

void funcName(StructName strct) {
  printf("%d\n", strct.field);
}

Will it be different from this one?
void funcName(StructName strct) {
  printf("%d\n", strct->field);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2017-10-19
@rsatarov

When working with a pointer to a structure, then use an arrow, and if the structure (reference) is directly used, use a dot:
StructName * strctptr;
strctpptr->field;
StructName strct;
strct.field;

D
Denis Zagaevsky, 2017-10-19
@zagayevskiy

In the case when you have a pointer to a structure, you need an arrow, otherwise - a dot. By the way, it seems that this is not C, but C ++.
strct->field is syntactic sugar for (*strct).field

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question