开发者

Understanding C: Incrementing pointers

开发者 https://www.devze.com 2023-03-08 18:53 出处:网络
I\'m studying for my exam in programming languages, and I stumbled upon this (written in C): *tp++ = *sp++;

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


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.


*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.


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:

  1. Fetch the value of sp (call it sp0)
  2. Fetch the value of tp (call it tp0)
  3. Calculate sp0 + 1 (call it sp1)
  4. Calculate tp0 + 1 (call it tp1)
  5. Store sp1 at sp
  6. Store tp1 at tp
  7. Use sp0 to fetch a value (call it v)
  8. Use tp0 to store v

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.


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++;


In

*tp++ = *sp++;

there are four things hapenning:

  • increment tp
  • increment sp
  • fetching the value at sp before increment
  • assigning that value at tp before increment

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++;
  • ...


They are incremented after the pointers addresses are equal to each other. If you don´t want that, you should use ++*tp and ++*sp.

0

精彩评论

暂无评论...
验证码 换一张
取 消