-1

This is the code, When I run this on vsode it runs smoothly without any warning, But running it on android apps like cxxdroid shows the warning at line printf("%d %d %d\n", (*p)++, (*p)++, *(++p)); unsequenced modification and access to 'p', and both the compiler returning different output as follows below main() body:

#include <stdio.h>

int main() {
    int a[] = {10, 11, -1, 56, 67, 5, 4};
    int *p, *q;
    p = a; 
    q = a + 3;

    printf("%d %d %d\n", (*p)++, (*p)++, *(++p));
    printf("%d\n", *p);
    printf("%d\n", (*p)++);
    printf("%d\n", (*p)++);
    q--;
    printf("%d\n", (*(q + 2))--);
    printf("%d\n", *(p + 2) - 2);
    for (int j = 0; j < 7; j++) {
        printf("%d, ", a[j]);
    }
    printf("%d\n", *(p++ -2) - 1);
    
    return 0;
}

vscode output:

12 11 11
13
13
14
67
54
10, 15, -1, 56, 66, 5, 4, 198

cxxdroid output:

10 11 11
11
11
12
67
54
12, 13, -1, 56, 66, 5, 4, 198
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 7
    The order of argument execution is not specified. If you need a specific order, do them as separate statements. – Barmar Apr 22 '22 at 16:07
  • On an unrelated note, you do know that for any pointer or array `p` and index `i`, the expression `*(p + i)` is *exactly* equal to `p[i]`? If not, then now you know. And the "normal" Array indexing syntax is easier to read and understand, and less to write. Please use it instead of direct pointer arithmetic. – Some programmer dude Apr 22 '22 at 16:11
  • 1
    Please show your expected/desired output. The code has both unspecified and undefined behavior. – Ian Abbott Apr 22 '22 at 16:16
  • One compiler is warning you about undefined behaviour the other did not, but it is still undefined. Warnings are not mandatory diagnostics, and can be suppressed or enabled in most compilers. So the fact that you get both differing diagnostics and different results should not be surprising. The take home here is not to write code like that. – Clifford Apr 22 '22 at 22:07

2 Answers2

3

Simply change

printf("%d %d %d\n", (*p)++, (*p)++, *(++p));

to

printf("%d ", (*p)++);
printf("%d ", (*p)++);
printf("%d\n", *(++p));

and the like.

Within a single expression, like printf("%d %d %d\n", (*p)++, (*p)++, *(++p)), there are very few guarantees about evaluation order. But when you have completely separate statements (that is, with semicolons ; in between them), the order of evaluation of those statements is perfectly guaranteed.

See also Why are these constructs using pre and post-increment undefined behavior?
See also Why does a=(b++) have the same behavior as a=b++?

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
1

To change printf("%d %d %d\n", (*p)++, (*p)++, *(++p)); to have a defined order of evaluation that you select, use temporary variables:

int argument1 = (*p)++;
int argument2 = (*p)++;
int argument3 = *(++p);
printf("%d %d %d\n", argument1, argument2, argument3);

Put the first three statements in the order you choose.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Like if I put arg3 above arg1 in editor, then printf() will evaluate arg3 first, Printf will give highest priority to those args which are written first and evaluate the same. right? –  Apr 22 '22 at 16:29
  • @SatyamS If you compute and assign a value to `argument3` first, the *compiler* will give it priority, in that it will do it first. Statements are always executed in order, from top to bottom (unless loops,`goto` statements, or function calls change that order). But printf is not giving higher priority to anything. – Steve Summit Apr 22 '22 at 16:56