Tuesday 25 April 2017

c - What is '-1[p]' when p points to an array (of int) index?

Today I stumbled over a C riddle that got a new surprise for me.




I didn't think that -1[p] in the example below would compile, but it did. In fact, x ends up to be -3.



    int x;
int array[] = {1, 2, 3};
int *p = &array[1];
x = -1[p]


I searched the internet for something like -1[pointer] but couldn't find anything. Okay, it is difficult to enter the correct search query, I admit. Who knows why -1[p] compiles and X becomes -3?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...