It was thus said that the Great der Mouse once stated:
But really, it gets to me when I see code like:
for (i = 0 ; i < MAX ; i++)
foo[i] = 0;
when it could easily be replaced with:
memset(foo,0,sizeof(foo));
While I realize you said "when", I would point out that this is a valid
replacement only when foo is an integral type. If foo is float or
pointer, this is not a safe thing to do (except in code that's
sufficiently hardware-specific that you can assume the in-memory layout
of the relevant data type - and know that all-0-bits is TRT).
Which is why I wish C supported a syntax of:
sometype_t foo [MAX];
foo = SOME_VALUE;
But then, that wouldn't be C anymore 8-P
Also, note that this assumes that sizeof(foo) is
MAX*sizeof(foo[0]),
though I realize this is easy to fix in the memset call.
If foo is declared as
sometype_t foo [MAX];
then sizeof(foo) == sizeof(sometype_t) * MAX
But if foo is declared as:
sometype_t *foo;
then yes, sizeof(foo) != sizeof(sometype_t) * MAX
-spc (Who still writes CGI based programs in C ... )