|
-
August 2nd, 2010, 09:38 PM
#3
Re: A question regarding *s++
Thanks for your explainations.
 Originally Posted by Lindley
It is actually incremented first. However, note that the postfix version of ++ is used, which means that while s itself is incremented, the return value of the operator is the old value of s.
That is,
Code:
char *s = "Hello";
// *s == 'H'
char *t = s++;
// *t == 'H', *s == 'e'
*t = 'h'
// now t points to "hello". Well, it doesn't, since modifying a string literal will probably crash the program, but it would if the Hello array had been in modifiable memory to begin with.
This behavior is the reason why prefix increment, ++s, should be preferred unless you have a specific need for the postfix version. It's not only less confusing, it can in some cases be more efficient.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|