Why is ptr + 1 different from ++ ptr in this scenario?

I originally had the following code where it- void**:

while (it != end)
{
    *it = *(it + 1);
    it++;
}

I decided to change it + 1; it++;to ++it;because I calculated the same value twice.

while (it != end)
{
    *it = *(++it);
}

However, after that, my program no longer works the same.

Edit: I would like to point out that this is not i = i++;, but rather an assignment to the value that the itvalue before it points to.

+6
source share
2 answers

*it = *(++it); it *it it ++it. , it *it. C , . , *it, , - . .

(++it) ( *it) . undefined. C 2011 [N1570] 6.5 2:

, undefined.

it ++it , it *it - it, . undefined.

+4

C99, 6.5 :

  1. , . , , .

( .)

*it = *(++it);

. *it it; ++it it. .

undefined, .

*it = *(it + 1); , it ( ), , .

+2

All Articles