clearing a char array c

后端 未结 16 817
挽巷
挽巷 2020-12-07 10:31

I thought by setting the first element to a null would clear the entire contents of a char array.

char my_custom_data[40] = \"Hello!\";
my_custom_data[0] = \         


        
16条回答
  •  伪装坚强ぢ
    2020-12-07 10:58

    It depends on how you want to view the array. If you are viewing the array as a series of chars, then the only way to clear out the data is to touch every entry. memset is probably the most effective way to achieve this.

    On the other hand, if you are choosing to view this as a C/C++ null terminated string, setting the first byte to 0 will effectively clear the string.

提交回复
热议问题