I've seen two different ways to fill a char[]
with 0
(in this example, upon initialization):
/* method 1 */
char foo[1024] = {0};
/* method 2 */
char foo[1024];
memset(foo, '\0', sizeof(foo));
What are the main differences between the two codes? How do they differ in functionality, etc?
In case 1 the array is zeroed out from the moment it can be used, as it got initialised.
In case 2 its content is well defined only after the call to
memset()
.The essential difference is that for case 2 there is is gap where
foo
's content is "garbage". This isn't the case for case 1.