4

How to analyse the precedence in following situation .

for (i=0; i<20; i++)
{
    *array_p++ = i*i;
    printf("%d\n",*arr++);
}

how is following code different from above.

for (int i=0; i<20; i++)
{
    *arr = i*i;
    printf("%d\n",*arr);
    arr++; 
    printf("%d\n",(int )arr);
}

I am expecting same output but outputs are different for *arr value

JimLohse
  • 1,209
  • 4
  • 19
  • 44
Imposter
  • 2,666
  • 1
  • 21
  • 31

5 Answers5

4

Citing Wikipedia, postfix ++ binds before unary *. This means that you have *(arr++). For example, in the expression *arr++ = 5, *arr is assigned to 5, then arr is incremented.

In the K&R, this trick is used to write a concise version of memcpy. It's something like:

while (--size)
    *dest++ = *src++;

I'll post the correct example after I get home tonight.

Edit: Apparently, only postfix ++ has higher precedence. Wikipedia says prefix ++ has equal precedence.

Oscar Korz
  • 2,457
  • 1
  • 18
  • 18
4

Postfix operators have higher precedence than unary operators, so *x++ is parsed as *(x++); the result of the expression x++ (which is x) is dereferenced.

In the case of *++x, both * and ++ are unary operators and thus have the same precedence, so the operators are applied left-to-right, or *(++x); the result of the expression ++x (which is x + sizeof *x) is dereferenced.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • 3
    In case of *++x, the operators dereference and suffix are applied right-to-left, not left-to-right. http://en.cppreference.com/w/c/language/operator_precedence – Aymen Oct 25 '15 at 06:48
2

It is easy to remember which has precedence when you consider prefix increment.

what has precedence here?

*++array = x; // pretty obvious

the same precedence rules go for the postfix expression so its rather easy.

The same goes for the cast operator.

(somestruct *)x + 1;

first the cast is done since if it wasn't the following wouldnt make sense

x + (int)1;

Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
2

In the first sample code snippet,

    for (i=0; i<20; i++)
        {
          *array_p++ = i*i;
           printf("%d\n",*arr++);
    }

array_ptr is incremented first i.e address, and then the value computed from i*i is assigned to *array_ptr since evaluation takes in the order of right to left i.e *(array_ptr ++).

In the second sample of code snippet,

for (int i=0; i<20; i++)
{
  *arr = i*i;
printf("%d\n",*arr);
 arr++; 
printf("%d\n",(int )arr);
}

value computed from i*i is first computed and assigned to *arr, then pointer pointing to arr is incremented.

Hence there is difference in values among the 2 code snippets.

User001
  • 91
  • 8
1

Operators ++ and * have the same precedence (postfix or prefix).However the associativity in such cases is from right to left. so in cases like

*ptr++ ==>  *(ptr++) 
*++ptr ==> *(++ptr)
++*ptr ==> ++(*ptr)

this link should give you more information on operators precedence and their associativity.. http://www.isthe.com/chongo/tech/comp/c/c-precedence.html

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48