Operator precedence in c with pointers

后端 未结 5 1784
面向向阳花
面向向阳花 2020-12-30 19:13

How to analyse the precedence in following situation .

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

how

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 19:37

    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.

提交回复
热议问题