Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jiminaus

macrumors 65816
Original poster
Dec 16, 2010
1,449
1
Sydney
I thought I new C pretty well. Until today when I learnt C's && and || operators are short-circuiting. I've been needlessly writing nested if's for years.

Now I see in lloyddean's code he put up the in "Getting duplicate numbers" thread that he consistently uses the following code:

Code:
(*p)._end

where "p" is pointer to a structure with a member called "_end".

I would have expected the following instead:

Code:
p->_end

I thought these two were completely equivalent. But now I'm not sure. Are the exactly the same?
 
The -> is simply syntactic sugar for a pointer dereference

Now I see in lloyddean's code he put up the in "Getting duplicate numbers" thread that he consistently uses the following code:

Code:
(*p)._end

where "p" is pointer to a structure with a member called "_end".

I would have expected the following instead:

Code:
p->_end

I thought these two were completely equivalent. But now I'm not sure. Are the exactly the same?

The . operator is for direct member access.
The arrow dereferences a pointer so you can access the object/memory it is pointing to.
The . operator has greater precedence than the * operator, so . is evaluated first. That's why you need parenthesis in:
Code:
(*p)._end

Note that the -> operator cannot be used for certain things, for instance, accessing operator[].

Trebor.
 
The piece was meant to be somewhat instructional and required that particular usage in a couple of places so I thought it best to be consistent so as not to confuse overall.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.