2

just doing my Homeworks and discovered this piece

A[j]=A[j-1];
j--;

is there a way to simplify this to one line? edit one statement?

I've tried

A[j--]=A[j];

but it doesn't seem to work well.

the code is from an InsertSort algorithm

edit this question is not required to do my homework, i am just curious

Valerij
  • 27,090
  • 1
  • 26
  • 42
  • 5
    Better keep it like this or you will have to add a comment there to explain WTF is going on. – ismail Dec 28 '10 at 14:09
  • 4
    Worst. Homework problem. Ever. Now I know where all the UB code is coming from. – Derrick Turk Dec 28 '10 at 14:13
  • its not a homework problem i just found it there. and i dont want to use this i just want to know how its possible to write it as one statement – Valerij Dec 28 '10 at 14:20
  • 2
    http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – Prasoon Saurav Dec 28 '10 at 14:25
  • I would not bother even considering it. The resulting generated code (if you suceed) is unlikley to be any different and the rules for evaluation order of unary increment and decremnent are so arcane as to be best avoided (by sticking with your original code). If I were marking this homework and you attempted to do this I'd dock marks for bad practise. – Clifford Dec 28 '10 at 14:58
  • 1
    How would anything be more simple than the two liner you have quoted? Making it into a one liner would make it less simple! – David Heffernan Dec 28 '10 at 15:14
  • @Clifford: I'd dock 100% for UB, but if OP found a way to condense it without UB, I think I'd just write some comments and not mark it down. :-) – R.. GitHub STOP HELPING ICE Dec 28 '10 at 19:38

3 Answers3

7

From the standard:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

That is, A[j] = A[--j]; will result in undefined behavior. Don't do it. A[j]=A[j-1]; j--; is perfectly clear, concise, and satisfactory.

Thom Smith
  • 13,916
  • 6
  • 45
  • 91
6

If the goal is just to eliminate the ; in the middle so you can use this in a macro context or as a single statement without braces, try using the comma operator:

A[j]=A[j-1], j--;

or if you want the assigned value as the result of the expression:

j--, A[j+1]=A[j];

Both should generate identical code on a decent compiler if the result of the expression is not used.

As others have said, any attempt to do this without the comma operator will result in undefined behavior due to sequence point issues. If you don't have a good reason for condensing code like this, I would recommend not even doing it. Unless you're very experienced with C, you're almost sure to mess it up and introduce subtle bugs (some of which may manifest not with your current compiler, but in future versions of it, creating hell for whoever gets stuck debugging the code).

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
0

There is actually a way

A[j+1]=A[--j];

is works well in VC but causes UB on g++

Valerij
  • 27,090
  • 1
  • 26
  • 42
  • 1
    It always causes UB. The UB just happens to result in the effect you want on VC, but that doesn't change the fact that it's UB. Note that I said "the effect you want" and not "the right result" because there is no right result for this statement. – R.. GitHub STOP HELPING ICE Jan 27 '11 at 04:26
  • well i seem to misunderstood that UB is - i somehow thought it result in a crash or something like this – Valerij Mar 28 '11 at 23:02
  • 1
    UB means the language intentionally does not define what happens, and allows *anything* to happen - including the effect you wanted, or a useful but unwanted effect like a crash (which helps you debug), or silently-wrong behavior that's very difficult to track down, or even making your computer blow up (although a good OS will of course not allow the latter). – R.. GitHub STOP HELPING ICE Mar 28 '11 at 23:47