:?:The statement *s1++=*s2++ actually leads to the assignment of value at s2 to s1 followed by incrementation of s2 then the incrementation of s1.

Dani AI

Generated

Short answer: the expression in the first post is legal C and is a common idiom. Postfix ++ has higher precedence than unary *, so the increments apply to the pointer variables, and the dereference uses the original pointer values. The net effect is that the object pointed to by the source is copied into the object pointed to by the destination, and by the time the statement finishes both pointer variables have been advanced.

How it is evaluated (conceptually): the compiler obtains the lvalue that comes from dereferencing the pointer's original value and obtains the rvalue from dereferencing the other pointer's original value, then performs the assignment. The increments on the pointer variables are side effects that must be completed before the next sequence point (end of the full expression), but the standard does not guarantee which of those side effects happens first or exactly when during evaluation — only that they will have occurred by statement completion.

Important cautions: do not rely on any particular interleaving of the side effects. If the same pointer variable is modified more than once in the same expression (for example, using the same pointer on both sides with post-increment) the behavior is undefined. Also watch aliasing: if source and destination overlap in ways that make reads and writes touch the same object without clear sequencing, you can get surprises.

Practical advice tied to the thread: this explains 's copying idiom and matches 's point about sequencing; was right about precedence but mistaken to call the construct illegal. For clarity and maintainability, prefer explicitly separated steps (read into a temporary, assign, then advance pointers) when code readability or strict sequencing matters.

Recommended Answers

All 5 Replies

:?:The statement *s1++=*s2++ actually leads to the assignment of value at s2 to s1 followed by incrementation of s2 then the incrementation of s1.

The above statements is illegal since to assign the value to the variable it must be an L value and any expression is not an L value. SO the above stms are wrong. The left hand side of the expresssion can only be a variable and not any expression or constant address.

The precedence of the post increment operator (++) is higher than the dereference ooperator (*). So the above stmts actually increase the address which the pointers hold and not the value pointed by them.

So to get the actual operation done you shold try something like:

(*s1) = (*s2)++;
(*s1)++;

Hope it helped, bye.

> actually leads to the assignment of value at s2 to s1
Yes, s2 is dereferenced to yeild a value, and that value is stored to where s1 points.

> followed by incrementation of s2 then the incrementation of s1.
That's less clear
http://c-faq.com/expr/seqpoints.html
Which order they happen in, and at what point in the evaluation of the whole expression is left to the compiler implementer.
All that you know is that both WILL have happened by the time the statement is complete (the sequence point).

Which is why you should never peer too deeply into exactly how and when expressions with side effects apply their side effects.

The above statements is illegal .

No it isn't illegal --I use similar constructs frequently to copy data from destination to source. Example:

char buf1[20];

char* p1 = buf1;
char *p2 = "Hello World";

while(*p2)
  *p1++ = *p2++;
*p1 = 0;

The above statements is illegal since to assign the value to the variable it must be an L value and any expression is not an L value. SO the above stms are wrong. The left hand side of the expresssion can only be a variable and not any expression or constant address.

The precedence of the post increment operator (++) is higher than the dereference ooperator (*). So the above stmts actually increase the address which the pointers hold and not the value pointed by them.

So to get the actual operation done you shold try something like:

(*s1) = (*s2)++;
(*s1)++;

Hope it helped, bye.

Thanks for ur reply .
But the stmt is correct and moreover I want the pointer to be incremented not the values.

Err... sorry, i aplogise to both you and Mr. Ancient Dragon. I blindly assumed some things in my mind and gave the answer which is not what i usually do.

Sorry if i misled my friends at the forums.
Hope you guys forgive me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.