Search code examples
ctypeslvalue

lvalue and function result -- dependency?


Consider the following snippet:

int func(char *ptr);
...
n += func(p + n);

Does this code yield undefined behaviour, since the function's argument depends on lvalue? I'd assume that a compiler would calculate a result of function, and then increment p + n, or this probably is compiler specific?


Solution

  • There is a sequence point before a function enter in function call. It means that every value computation and the side effects associated with an argument are completed before function entered in a function call.

    C11-§6.5.2.2/10:

    There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call.

    So, in case of

    n += func(p + n);  
    

    p + n will be calculated before the function call.