Question with C pointers

The following code confused me a bit:

char * strcpy(char * p, const char * q) { while (*p++=*q++); //return } 

This is a split implementation of the strcpy function. From this code, we see that the pointer p and q grows, which is dereferenced, and q is assigned p until \ 0 char is reached. What confuses me is the first iteration of the while loop. As far as I know, the array pointer points to the first element of the array. In this code, the pointer is incremented before the assignment happens (on the right?), Therefore, from my understanding, the first element should never be assigned p (since the post-increment occurs before the assignment), but this happens and I do not understand the magic behind. Is anyone enough to explain why? :-)

thanks

+6
source share
8 answers

Since ++ is after variables, they do not increase until the expression is evaluated. This is why it is an operator after an increment; pre-increment prefix (++ p). * ++ p will write to the second spot, * p ++ writes to the first.

+9
source

p++ is a step indicator p . Thus, the current value of p controlled by the deference * operator until increment p .

Your reasoning would be correct if the while was written as follows:

 while (*++p=*++q); 

In this case, the increment will occur before dereferencing.

+2
source

No, the increment occurs after the appointment.

If it was *(++p) , the pointer p would be incremented and assigned after that.

+2
source

The expressions x++ and ++x have both a result (meaning) and a side effect.

The result of the expression x++ is the current value of x . A side effect is that the content of x increases by 1.

The result of ++x is the current value of x plus 1. The side effect is the same as above.

Please note that a side effect should not be applied immediately after evaluating an expression; it should only be applied to the next point in the sequence. For example, given the code

 x = 1; y = 2; z = ++x + y++; 

there is no guarantee that the contents of x will be modified before the y++ expression is evaluated, or even before the result of ++x + y++ assigned to z (neither = nor + operators enter a sequence point). The expression ++x evaluates to 2, but it is possible that the variable x may not contain the value 2 until z is assigned.

It is important to remember that the behavior of expressions like x++ + x++ clearly undefined by the locale; there is no (good) way to predict what the result of the expression will be, or what value x will contain after evaluating it.

Postfix operators take precedence over unary operators, so expressions like *p++ parsed as *(p++) (i.e. you apply the * operator to the result of a p++ expression). Again, the result of the p++ expression is the current value of p , so while (*p++=*q++); Do not skip the first item.

Note that the operand for auto-increment / decrement operators must be an lvalue (essentially an expression that refers to a memory location, so that the memory can be read or modified). The result of the expression x++ or ++x not an lvalue value, so you cannot write things like ++x++ or (x++)++ or ++(++x) . You could write something like ++(*p++) ( p++ not lvalue, but *p++ is), although this will probably make you slap everyone who reads your code.

+2
source

The right side of the expression (* q ++) will be evaluated to * p ++, and both will only increase after the assignment is completed.

Read the instructions from right to left and remember the post-increment (q ++ instead of ++ q) will happen after everything else in the line is resolved.

 *q --> dereference q = --> assign the value *p --> to p 

add both.

Do this until qp takes q element = 0 when it reaches the null terminator.

+1
source

This is a split implementation of the strcpy function. From this code, we see that the pointer p and q grows, which is dereferenced, and q is assigned p until \ 0 char is reached.

This is the opposite. The value in *p set to *q , then both pointers increase.

When you have int foo = bar++ , the increment happens after setting foo. For this to happen first, you would do int foo = ++bar

+1
source

q++ is q ++q is q+1

+1
source

The while condition performs a post-increment. Equivalent:

 while (true) { char* old_p = p; const char* old_q = q; ++p; // or p++; ++q; // or q++; *old_p = *old_q; if (*old_p == '\0') break; } 
+1
source

Source: https://habr.com/ru/post/896244/


All Articles