Answer the question
In order to leave comments, you need to log in
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);
}
void funcName(StructName strct) {
printf("%d\n", strct->field);
}
Answer the question
In order to leave comments, you need to log in
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;
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 questionAsk a Question
731 491 924 answers to any question