Why size_t when int would suffice for the size of an array?

后端 未结 5 1869
[愿得一人]
[愿得一人] 2021-01-17 13:09

The C standard guarantees that an int is able to store every possible array size. At least, that\'s what I understand from reading §6.5.2.1, subsection 1 (Array

5条回答
  •  隐瞒了意图╮
    2021-01-17 13:53

    The term "integer type" doesn't mean int - for example, char, and short are integer types.

    Just because you can use an int to subscript an array doesn't necessarily mean that it can reach all possible array elements.

    More specifically about size_t vs. int, one example would be platforms where int might be a 16-bit type and size_t might be a 32-bit type (or the more common 32-bit int vs 64 bit size_t difference on today's 64-bit platforms).

提交回复
热议问题