clearing a char array c

后端 未结 16 811
挽巷
挽巷 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:46

    Nope. All you are doing is setting the first value to '\0' or 0.

    If you are working with null terminated strings, then in the first example, you'll get behavior that mimics what you expect, however the memory is still set.

    If you want to clear the memory without using memset, use a for loop.

提交回复
热议问题