Questions for the order.
Edit
for(;*newptr[j] < *newptr[j+1] && j!=0; j--)
in
for(; j != -1 && *newptr[j] < *newptr[j+1]; j--)
Presumably an error is what causes the code to crash. This is because the expression in the for loop evaluates from left to right. Therefore, *newptr[j] is evaluated to if j != -1 . Therefore, we can assume that at some point j is -1 when *newptr[j] is evaluated, which is illegal.
Reordering matters for a second reason: short circuit assessment .
When evaluating two expressions made from two conditions A and B , C ++ does not always need to evaluate both conditions.
For example, in a statement
if (A && B) { //do something }
if A evaluates to false , then obviously A && B cannot evaluate to true no matter what evaluates to B Therefore, the value of B never checked. So, in your case, in the expression
j != -1 && *newptr[j] < *newptr[j+1]
if j != -1 is false, C ++ does not need to evaluate the rest of the expression to know that the whole expression is false. So *newptr[j] never happens, and you don't get an error.
source share