0

I faced the following code problem. When I run this code in Mac, I get the output as 1 2. However, on Linux command line, the output is 2 2. Can anyone please explain why this may be happening?

struct point
{
    int x,y; 
};

void foo(struct point p[])
{
    printf("%d %d", p->x, ++p->x);
}

int main()
{
    struct point p1[ ] = {1, 2, 3, 4}; 
    foo(p1);

    return 0;
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
Nimish Mishra
  • 143
  • 1
  • 1
  • 5
  • 4
    the order of evaluation of arguments is up to the vendor to implement: `printf("%d %d", p->x, ++p->x);` so it's allowed for `p->x` or `++p->x` to be evaluated before the other, in other words, it can be evaluated left to right or right to left order. The standard does not define the order of evaluation. So your compilers being different is down to the implementation of this being different – EdChum Mar 26 '19 at 13:39
  • Thank you for the explanation. This really worked. – Nimish Mishra Mar 26 '19 at 14:10

0 Answers0