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

前端 未结 9 627
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  爱一瞬间的悲伤
    2020-12-30 10:24

    You're asking about C specifically, but C++ builds upon this as well:

    Most pointer arithmetic naturally generalizes to the Forward Iterator concept. Walking through memory with *p++ can be used for any sequenced container (linked list, skip list, vector, binary tree, B tree, etc), thanks to operator overloading.

提交回复
热议问题