What are convincing examples where pointer arithmetic is preferable to array subscripting?

前端 未结 9 621
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 09:23

I\'m preparing some slides for an introductory C class, and I\'m trying to present good examples (and motivation) for using pointer arithmetic over array subscripting.

9条回答
  •  -上瘾入骨i
    2020-12-30 10:11

    char *my_strcpy(const char *s, char *t) {
      char *u = t;
      while (*t++ = *s++);
      return u;
    }
    

    Why would you want to spoil such a beauty with an index? (See K&R, and how they build on up to this style.)There is a reason I used the above signature the way it is. Stop editing without asking for a clarification first. For those who think they know, look up the present signature -- you missed a few restrict qualifications.

    Structure alignment testing and the offsetof macro implementation.

提交回复
热议问题