-2

In the code below, I'm confused about the working of 3 line.If anyone explains it ,I will be very grateful. Thank you.

#include< iostream >
using namespace std;

int main()
{
1. char s[]="abcdef";
2. char *p=s;
3. *p++=*++p;
4. cout<<s;
5. return 0;
}
273K
  • 29,503
  • 10
  • 41
  • 64
  • There is no working of 3 line. That line doesn't compile. – Igor Tandetnik Jul 12 '20 at 03:17
  • Which C++ standard are you compiling to? (Or what's your compilation command?) – chris Jul 12 '20 at 03:23
  • By the way, current versions of both GCC and Clang [warn here](https://gcc.godbolt.org/z/MvhxPe) when compiling below C++17. – chris Jul 12 '20 at 03:38
  • You shall not modify one variable mutiple times in a sentence. This is undefined behavior and will cause different output in different compilers. Split it. – H3d9 Jul 12 '20 at 04:20
  • See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696). Developing good habits now it a lot easier than breaking bad ones later... – David C. Rankin Jul 12 '20 at 04:28
  • See [C++ - Order of evaluation](https://en.cppreference.com/w/cpp/language/eval_order) – David C. Rankin Jul 12 '20 at 04:42
  • Post increment operator returns rvalue and preincrement operator returns lvalue. So technically doing something very dependent on compiler. On VisualC++ it probably assigns the third element to first. – armagedescu Jul 12 '20 at 06:31

1 Answers1

1

Before the line *p++=*++p;, p points to the start of the array s.

The ++p in the right hand-side of the line will increment p by one and so it now points to b. Dereferencing it with * will give the value b and so the right hand side evaluates to b.

In the left hand side, since the post-increment operator is used, the value of p will not immediately change. Thus, the value b from the RHS will be set to the same memory location. Because of the post-increment operator, p will point to the character c of the array.

Thus, the char array will remain the same after this line and so abcdef will be printed. p however will point to the character c of the array.

Note that the above is valid only since c++17. From en.cppreference.com/w/cpp/language/eval_order: In every simple assignment expression E1=E2 and every compound assignment expression E1@=E2, every value computation and side-effect of E2 is sequenced before every value computation and side effect of E1

H Krishnan
  • 896
  • 9
  • 12
  • Considering the [Compiler warning](https://gcc.godbolt.org/z/MvhxPe) mentioned by @chris and C++’s sequencing rules, are you really certain that your explanation is correct and no undefined behaviour is invoked? If you are, it would be really helpful if you added an explanation to the answer. This is a quite tricky corner of the language, after all. – besc Jul 12 '20 at 07:37
  • @besc From https://en.cppreference.com/w/cpp/language/eval_order: _In every simple assignment expression E1=E2 and every compound assignment expression E1@=E2, every value computation and side-effect of E2 is sequenced before every value computation and side effect of E1 (since c++17)_. Doesn't this imply that `*p++ = *++p;` is well-defined? – H Krishnan Jul 12 '20 at 18:18
  • should note that this is for C++17, which is when sequencing on assignment was added – Cubbi Jul 13 '20 at 14:57
  • @HKrishnan That certainly sounds convincing. It would be great if you added that explanation to the answer. I became skeptical because GCC warns even in C++17 mode. Clang doesn’t though, so maybe GCC is overzealous. – besc Jul 13 '20 at 15:40