C - when to use pointer arithmetic, when to use array indexing?

前端 未结 6 1425
盖世英雄少女心
盖世英雄少女心 2020-12-08 11:38

In C, when is it preferrable to use one over the other?

6条回答
  •  既然无缘
    2020-12-08 12:15

    It is really a matter of style and of coding conventions, since in C p[i] is defined to be the same as *(p+i) where p is a pointer and i an integral index. AFAIK you could even write i[p] but that is ugly.

    This is not always true in C++ (which gives you the power of defining operator [] etc...).

    I personally dislike hand-coding &p[i] and prefer p+i in that case.

提交回复
热议问题