Does pointer arithmetic still work outside the array?

前端 未结 3 1851
孤街浪徒
孤街浪徒 2021-01-04 19:43

I am always reading that pointer arithmetic is defined as long as you don\'t leave the bounds of the array. I am not sure I completely understand what this means and I was a

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 20:32

    "Yes" the conditions you mentioned are covered in specifications.

    
        int *r = p - 2; 
    
    

    r is outside bounds of array p, the evaluation results in allocation of position to r, 2 int positions behind/before the address of p.

    `r[3]` is simply the "4th" int position after the address of r

提交回复
热议问题