1

I recently came along this line of code:

CustomData_em_free_block(&em->vdata, &eve->data);

And I thought, isn't:

a->b

just syntactic sugar for:

(*a).b

With that in mind, this line could be re-written as:

CustomData_em_free_block(&(*em).vdata, &(*eve).data);

If that's the case, what is the point of passing in

&(*a), as a parameter, and not just a? It seems like the pointer equivalent of -(-a) is being passed in in, is there any logic for this?

Thank you.

Leif Andersen
  • 21,580
  • 20
  • 67
  • 100

3 Answers3

5

This:

&(*em).vdata

is not the same as this:

em.vdata

It is the same as this:

&((*em).vdata)

The ampersand takes the address of vdata, which is a member of the struct pointed to by em. The . operator has higher precedence than the & operator.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • I agree with the unicorn part. Anyway, that makes sense. So in that case: (&(*a)).b is the same as: a.b, but without the extra () in there to change the order of operations, the dot product happens first, right? Thank you. – Leif Andersen Apr 01 '10 at 04:04
  • @Leif Andersen: Yes, `(&(*a)) == a`; that's the meaning of those two operators. And yes, the `.` has higher precedence than the `&`. It's not a dot product, though - it's the member selection operator. – Cascabel Apr 01 '10 at 04:14
  • @Leif Andersen: You can google around for C operator precedence; here's the table on wikipedia: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence – Cascabel Apr 01 '10 at 04:17
  • Hmm...okay, thanks. But the member selection operator is still the operator used to get a particular variable from a struct, yes? (I always called that the dot operator, or possibly dot product, even though the latter could be confused with vectors in mathematics), thanks for making me aware of the notation. – Leif Andersen Apr 01 '10 at 04:28
  • @Leif: Yes, it is used to get a particular variable from a struct. It's acceptable to call it the "dot operator," but it _cannot_ be called the "dot product operator." – James McNellis Apr 01 '10 at 04:49
1

Here's what you're missing: &em->vdata is the same as &(em->vdata), not (&em)->vdata. That is, it's the address of the vdata member of the structure pointed to by em. This should be clear if you look at the type of em - it's a pointer.

Yes, you can always rewrite a_ptr->member as (*a_ptr).member, but why bother? That's the point of the syntactic sugar.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
-1

"If that's the case, what is the point of passing in &(*a), as a parameter, and not just a?"

Usually none. But notice that your sample code doesn't compare &(*a) and a. Your sample code compares &(*a) and b, where b is offset from a by whatever the distance is from the start of em to em's vdata member.

Windows programmer
  • 7,871
  • 1
  • 22
  • 23