:?: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.

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.