Are negative array indexes allowed in C?

后端 未结 8 2194
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 13:56

I was just reading some code and found that the person was using arr[-2] to access the 2nd element before the arr, like so:

|a|b|c|         


        
8条回答
  •  粉色の甜心
    2020-11-22 14:47

    #include 
    
    int main() // negative index
    { 
        int i = 1, a[5] = {10, 20, 30, 40, 50};
        int* mid = &a[5]; //legal;address,not element there
        for(; i < 6; ++i)
        printf(" mid[ %d ] = %d;", -i, mid[-i]);
    }
    

提交回复
热议问题