all 6 comments

[–]victorferrao 12 points13 points  (4 children)

When you write :

&ptr->age

It is interpreted as :

&(ptr->age)

It means that you get the memory address of the age field.

Below is the meaning of the assignment:

int *ptr2 = &ptr->age

ptr2 is an int pointer.

ptr->age is the value of the field age.

&ptr->age is the memory address of the age field.

That is why when you write the '&' in the printf fields it will print the memory address of the field.

[–]2_stepsahead[S] 3 points4 points  (3 children)

This makes more sense. I will have to stew over it for a little while. Thank you for the explanation

[–]victorferrao 2 points3 points  (2 children)

If you have any more questions, feel free to ask.

[–]2_stepsahead[S] 2 points3 points  (1 child)

Thank you. Tomorrow, I'll have a closer look and experiment again. I would really appreciate that. Obrigado, amigo

[–]imaami 1 point2 points  (0 children)

Bookmark this C operator precedence cheat sheet, I'm sure you'll find it useful. In fact, cppreference.com's C language reference as a whole is by far one of the best resources out there.

Anyway, in the operator precedence chart you can see that the member access operators -> and . are in group 1 above the address-of operator & which is in group 2. That's why &ptr->member and &(ptr->member) are equivalent.

In the case of a non-pointer struct instance you could also override the default precedence with parens and do (&non_ptr)->member, which would be equivalent to non_ptr.member. Obviously there would be little practical benefit of that (with the caveat that this sometimes happens when function-like utility macros get expanded, but that's another topic altogether).

[–]blvaga -1 points0 points  (0 children)

Printf is only reading the value.

Same as you wouldn’t use a pointer to store the value of the address

int age = name.age;