Search code examples
cincrementdereference

understand complex expression in C


Help me please understand the following expression:

(seen in a book)

*((int *)marks + i++) = i+1

A large number of increments and symbols dereference confusing!


Solution

  • (int *)marks
    

    cast marks to a int pointer

    + i++
    

    add i at marks (chaging the address pointed) and then increments i by 1

    *(...) = i+1
    

    set the VALUE of the cell pointed by our pointer to i+1 (pay attention at the fact that i as already been incremented before, so it will be greater of 2 by the start of the instruction.

    I hope this is an example f how you should NOT write the code :)