tags:

views:

166

answers:

4

If I declare the following variables:

int array[10] = { 34, 43,12, 67, 34, 43,26, 98, 423,1 };
int * p = array;

Then, this loop:

for ( int i = 0; i < 10; i++ )
{
    std::cout << &*p++ << " ";
}

gives me different output ( a different set of addresses ), to this code:

for ( int i = 0; i < 10; i++ )
{
    std::cout << p++ << " ";
}

Why? Aren't they semantically equivalent?

EDIT:

Well, my apologies to everyone that answered this one, I don't have the original code, it was a test that I did at home and it turns out that I deleted that code from my project. ( my broadband is not yet connected, so I waited till I got to work to post this ). Anyway - I am pretty sure that I was forgetting to initialise p. But the question of "aren't they semantically equivalent?" has been answered. Thanks.

A: 

The order of precedence is '++' first, then '*' and finally '&'.

So p++ will output the adresse of array[0] and &*p++ will first increment p, but this is postfix ! So the value of p (and not the value of p+1) will be given to * and then to &, so these are the same

Example:

std::cout << p << std::endl; // Output the adress of p
std::cout << &*p++<<std::endl; // p is increment but it is postfix, so value of p is used and printed
std::cout << &*++p<<std::endl; // p has been increment before and is then incremented again
std::cout << p++ << std::endl; // p has been incremented before, but here p is used first, then incremented
Cedric H.
Oli Charlesworth
I think `++` will be evaluated first.
Cedric H.
No, it isn't. That's the whole point of having prefix and postfix unary operators!
Oli Charlesworth
usta
Cedric H.
Chubsdad
@Oli Charlesworth Actually postfix ++ evaluates first here, but the old value is used for dereference, not the new value that increment produced.
usta
@Chubsdad: that's not what http://www.cppreference.com/wiki/operator_precedence says.
Cedric H.
I corrected my post to make it more clear ... What about the example ? I think the reasoning is correct.
Cedric H.
@Cedric: Your post is now correct, but now doesn't address the question!
Oli Charlesworth
@usta: I don't believe the order of precise evaluation is specified, because there isn't a sequence point.
Oli Charlesworth
Stephen
usta
usta
@usta: Agree with that.
Oli Charlesworth
Okay guys, I read your arguments, I started with an idea in mind at the beginning which was not correct, then I understood your arguments but without correcting everything, resulting in a messy, incorrect post.
Cedric H.
+13  A: 
int array[10] = { 34, 43,12, 67, 34, 43,26, 98, 423,1 };
int * p = array;

for ( int i = 0; i < 10; i++ )
{
    std::cout << p++ << " ";
}
p = array;
std::cout << '\n';
for ( int i = 0; i < 10; i++ )
{
    std::cout << &*p++ << " ";
}
std::cout << '\n';

Gives me the same addresses. Did you accidentally forget p = array; in between?

usta
+4  A: 

If you remember to reset p before the second loop, they give the same result.

Oli Charlesworth
A: 

reset the pointer p's position.

zubinmehta