-1

I was confused by the expression *d++=*s++. How to undertand it.

int main()
{
    char s[20]="hello,world";
    char d[20];
    char *src=s;
    char *des=d;
    while(*src) *des++=*src++;

    return 0;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
storen
  • 1,025
  • 10
  • 22
  • 3
    Not again - somebody writing code that is incomprehensible and difficult to understand and therefore requires asking a question on SO. Why not separate it into multiple lines so that it is readable? – Ed Heal Nov 25 '14 at 03:19
  • Possible duplicate of [Does *p++ increment after dereferencing?](http://stackoverflow.com/q/9255147/1708801) – Shafik Yaghmour Nov 25 '14 at 03:20
  • 1
    Why don't you put a `printf()` before `*des++=*src++` and after and observer the changes of `src` and `des` of the pointer position and values? It let you have a better understanding how it runs. – SSC Nov 25 '14 at 03:23
  • 1
    @Ed, it can't be incomprehendable _and_ difficult to understand - the former indicates that it's not difficult to understand but _impossible_ :-) Sorry, couldn't resist. In any case, if it's meant to copy the array or a C string, it's also wrong: in both cases it doesn't copy enough (the whole array for an array copy, the terminator for a string). – paxdiablo Nov 25 '14 at 03:23
  • 3
    @EdHeal: `*des++=*src++`, while it may be terse, is a pretty standard C idiom. I'm pretty sure it appears in K&R I. – Nate Eldredge Nov 25 '14 at 03:44

1 Answers1

5

It has the same behavior as:

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

That is copy the character pointed at by src to the character pointed at by dest. Then move each pointer to the next character element.

ouah
  • 142,963
  • 15
  • 272
  • 331