Search code examples
cstringoverwrite

What's the best way to reset a char[] in C?


I use a string:

char    word[100];

I add some chars to each position starting at 0. But then I need be able to clear all those positions so that I can start add new characters starting at 0.

If I don't do that then if the second string is shorten then the first one I end up having extra characters from the first string when adding the second one since I don't overwrite them.


Solution

  • If you want to zero out the whole array, you can:

    memset(word, 0, sizeof(word));