Copy string in C

I got confused in this code: ( http://www.joelonsoftware.com/articles/CollegeAdvice.html )

while (*s++ = *t++); 

What is the order of execution? First * s = * t is executed, and then each of them increases? Or vice versa?

Thanks.

EDIT: And what if it was:

 while(*(s++) = *(t++)); 

and

 while(++*s = ++*t); 
+7
c string pointers pointer-arithmetic
source share
7 answers
 while (*s++ = *t++); 

From the priority table, you can clearly see that ++ has a higher priority than * . But ++ used here as the post increment operator, so the increment occurs after the assignment expression. So, first *s = *t , then s and t increase.

EDIT:

 while(*(s++) = *(t++)); 

Same as above. You make it more explicit with parentheses. But remember that ++ is still a incremental increment.

 while(++*s = ++*t); 

There is only one statement next to s. So, * is applied first, and ++ is applied to this result, which leads to the lvalue required error.

 while(*++s = *++t); 

Again, just the operator next to s, t. Thus, an increment occurs first, and then a copy. Thus, we actually skip a copy of the first char from t to s.

+14
source share

You're right. * s = * t is executed first, and then they increase.

+4
source share

Increment is a step by step. A message is not only because it arrives after the variable is incremented, but also because it occurs after evaluating the expression. Thus, the execution order

 *s = *t 

then s ++ and t ++

+2
source share

EDIT ::

@chrisgoyal

The order of execution is an ambiguous term. There are two different things here. The syntactic order and semantics of expression.

Syntax first applies the ++ operator. If * s is applied first, then the following is equivalent to what @Hogan said:

 (*s)++ = (*t)++ 

Which is very different from the Joel pattern.

The semantics of the ++ operator are that it is executed after the expression.

Hope that clarifies that I am meat.


In fact, s++ and t++ are applied first. Remember that the post-fix statement is executed after the expression is executed. Basically, the ++ operator is applied to both, then *s = *t is executed.

+1
source share

In the "Operation increment" field, the first operation is used, and then after changing it.

0
source share

So there are two forms of increment

 ++s // increment before using value s++ // increment after using value 

And their result can be dereferenced:

 *++s // or... *s++ 

This works well on one of the first machines for C, the PDP-11, which had an indirect registration mode that incremented the register after. The following operating systems were available in hardware:

 *--s // or *s++ 

You can do either

 *x++ = *y++; // or *--x = *--y; // or some combination 

And if you did, the entire line occurred in one command. Since // comments were introduced by C99, however, you could not get away with the comment syntax.

0
source share

Code: (while *s++ = *t++); approximately equivalent:

 while (*s = *t) { ++s; ++t; } 

The second is exactly the same - additional partners do not change anything (in this case). In order for the paranas to not do anything, they would have to be like this: while ((*s)++ = (*t)++); . This will be about the same as your third example (described in the paragraph below).

Last example: while(++*s = ++*t); completely different. Since dereferencing ( * ) is closer to the operand, this raises the operand and increases the dereferencing result, which means that it increases what the pointer points to AT, rather than increasing the pointer itself. As a result, this will copy the first character, and then increment that character, and then check if that character was nonzero and continued until it became zero. The result will be both the source and destination, which will become an empty string (since the first character of both will now be zero, which is used to end the lines).

0
source share

All Articles