How to empty a char array?

后端 未结 13 1774
抹茶落季
抹茶落季 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:10

    using

      memset(members, 0, 255);
    

    in general

      memset(members, 0, sizeof members);
    

    if the array is in scope, or

      memset(members, 0, nMembers * (sizeof members[0]) );
    

    if you only have the pointer value, and nMembers is the number of elements in the array.


    EDIT Of course, now the requirement has changed from the generic task of clearing an array to purely resetting a string, memset is overkill and just zeroing the first element suffices (as noted in other answers).


    EDIT In order to use memset, you have to include string.h.

    0 讨论(0)
提交回复
热议问题