How to empty a char array?

后端 未结 13 1919
抹茶落季
抹茶落季 2021-01-29 19:33

Have an array of chars like char members[255]. How can I empty it completely without using a loop?

char members[255];

By \"empty\" I mean that

13条回答
  •  既然无缘
    2021-01-29 20:04

    You cannot empty an array as such, it always contains the same amount of data.

    In a bigger context the data in the array may represent an empty list of items, but that has to be defined in addition to the array. The most common ways to do this is to keep a count of valid items (see the answer by pmg) or for strings to terminate them with a zero character (the answer by Felix). There are also more complicated ways, for example a ring buffer uses two indices for the positions where data is added and removed.

提交回复
热议问题