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

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

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

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 12:20

    Pointers are useful when the size of the data structure is not known at compile time. e.g. when you do not know what is the length of a string, or how many integers you are expecting and so on. In such situations, pointers can be dynamically allocated memory depending on the requirements.

    Arrays on the other hand, reduce the flexibility.

    There is much more beyond this difference though.

提交回复
热议问题