-1
void mystrcat(char* dest,char* src)
{
    while(*dest) dest++;
    while(*dest++=*src++);
    return;
}

The above block of code is an user-defined function to copy the contents of a string to the end of another string.
Here, we walk through the destination string until it hits the null character '\0'. The 2nd while loop is supposedly used to copy the contents the source string to the end of destination string.

I have read that an expression such as, *ptr++ is evaluated as *(ptr++)according to the precedence table of operators in C.
If that is the case shouldn't the expression: *dest++=*src++ be evaluated as
*(dest++)=*(src++) ?
Wouldn't that cause dest to first point to the next location in the memory and updating its value rather than, updating '\0' with a character from the source string? Similarly, won't it cause src to miss the 1st character of the source string?

However, the function seems to be successful in copying the contents of the source string to the end of the destination string.

Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

1

With the postfix ++ operator the operand is incremented after it is evaluated in the current context. In this expression, the current value of dest is dereferenced and the location assigned to, and the current value of src is dereferenced and the value assigned to the left hand side.

From section 6.5.2.4 of the C standard:

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it). See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers. The value computation of the result is sequenced before the side effect of updating the stored value of the operand. With respect to an indeterminately-sequenced function call, the operation of postfix ++ is a single evaluation. Postfix ++ on an object with atomic type is a read-modify-write operation with memory_order_seq_cst memory order semantics.

So this:

*dest++=*src++

Is (roughly) the same as:

*dest=*src;
dest+=1;
src+=1;
dbush
  • 205,898
  • 23
  • 218
  • 273
0

What you're missing is that *dest++ uses post-increment. That means the expression dest++ returns the value from before it was incremented, and this is the pointer we dereference with *. So it assigns to the current location in memory, then increments the pointer.

*dest++ = *src++;

is effectively equivalent to:

*dest = *src;
dest++;
src++;

The problem you describe would happen if you instead wrote:

*(++dest) = *(++src);

because pre-increment increments the variable and evaluates to the new value, which is the next location.

See What is the difference between ++i and i++?

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • @TirthaSarathiGhosh Since post-increment has higher precedence it's equivalent to `*(dest++)`. If dereferencing had higher precedence it would be equivalent to `(*dest)++` – Barmar May 19 '17 at 22:35
  • Sorry my use of parentheses was wrong. The evaluation should be *(dest++) instead of (*dest)++ since, post-increment has higher precedence. –  May 19 '17 at 22:41
  • @TirthaSarathiGhosh And that's exactly what it is. That's what you wrote in the question. – Barmar May 19 '17 at 22:41
  • Please reread the last few points of the question. I clearly stated that evaluation of *(dest++) instead of (*dest)++ would cause dest to first point to the next location in the memory and updating its value rather than, updating '\0' with a character from the source string. –  May 19 '17 at 22:50
  • I explained why that's not true in my answer. `dest++` is evaluated first, but its value is the OLD value of `dest`. Then we dereference the old value with `*`, so we update the current location, not the next location. – Barmar May 19 '17 at 22:52
  • Ah! Got it! Thanks for the explanation. –  May 19 '17 at 23:05
0

It is important to note that a postfix increment or decrement expression evaluates to the value of the expression prior to application of the respective operator. The increment or decrement operation occurs after the operand is evaluated. See: Postfix Increment and Decrement Operators and confusion regarding *(asterisk) and ++ in c

Community
  • 1
  • 1
wyc
  • 351
  • 2
  • 11