How to memset char array with null terminating character?

Options one and two are just wrong. The first one uses the size of a pointer instead of the size of the array, so it probably won’t write to the whole array. The second uses sizeof(char*) instead of sizeof(char) so it will write past the end of the array. Option 3 is okay. You could also use this

memset( buffer, '\0', sizeof(char)*ARRAY_LENGTH );

but sizeof(char) is guaranteed to be 1.

Leave a Comment