Reputation: 4631
I'm studying for my exam in programming languages, and I stumbled upon this (written in C):
*tp++ = *sp++
;
I understood what *tp = *sp
; might do, but here, when are the pointers incremented? before, after the values are fetched? in what order? I appreciate your answers
Upvotes: 1
Views: 220
Reputation: 2454
You should be careful about believing the word 'after'. It is true that in this example, the behaviour is as if the pointers are incremented after the assignment, but the C compiler is under no obligation to actually do things in that order, as long as the result is the same.
Specifically, there are the following steps:
The only constraints on the order are the obvious ones, that values must be available before they're used. You can't know, and you shouldn't try to guess, what order the compiler will pick. On the other hand, the semicolon (statement terminator) is a 'sequence point', telling the compiler that it is not allowed to reorder effects across it -- unless it can prove that it does not change the result (but might be faster, for instance).
That is all mostly relevant if you try to use the same pointer variable more than once in the same statement, but even in your example it can make a difference if your pointers happen to point to themselves.
Upvotes: 3
Reputation: 108986
In
*tp++ = *sp++;
there are four things hapenning:
tp
sp
As long as the result is correct, any order is valid (it just depends on QOI)
*tp = *sp; tp++; sp++;
savedtp_register = tp; tp++; *savedtp_register = *sp; sp++;
savedsp_register = *sp; sp++; *tp = savedsp_register; tp++;
Upvotes: 1
Reputation: 300759
The ++
are post-increment operators, since they immediately appear after the pointers (rather than immediately before); both pointers are incremented after the assignment (of the de-referenced addresses).
It's equivalent to:
*tp = *sp;
tp++;
sp++;
Upvotes: 1
Reputation: 48121
Since the ++
operator comes after the variables, the values will be incremented after the expression is evaluated. So this will assign the value currently pointed to by sp
to the location currently pointed to bytp
, then increment both pointers.
If the expression was instead
*(++tp) = *(++sp)
Then the pointers would be incremented before evaluation.
These constructs commonly occur inside loops.
Upvotes: 7
Reputation: 30341
*tp++ = *sp++;
is equivalent to
*tp = *sp;
tp++;
sp++;
so it copies the value pointed to by sp
to the location pointed to by tp
and then increment both pointers.
Upvotes: 3
Reputation: 11961
They are incremented after the pointers addresses are equal to each other. If you don´t want that, you should use ++*tp
and ++*sp
.
Upvotes: -1