gw7rib
8/12/2011 2:40:00 PM
On Aug 12, 11:59 am, j...@toerring.de (Jens Thoms Toerring) wrote:
> Rox <roxxette...@gmail.com> wrote:
> > So I presume:
> > char c[10] = "";
> > Will initialize c into 0. Just like memset do.
> > It's such a neat way to initial a string that I can't believe it!
> > Am I right?
>
> If I'm not completely mistakent his means that this trick does
> not only work for char arrays but for all kinds of arrays. So
>
> int i[ 10 ] = { 1 };
>
> will initialize the first element of i to 1 and the rest to 0.
> (And if you use 0 as the only initializer you will get an array
> with all 10 elements set to 0.)
While you're both right (unless I too am mistaken!) I think Rox's
result is more surprising. I think one would expect initializing an
int array to initialize all the elements of that array. Whereas, if a
character array is intended to be initialised to a particular string,
one might expect that putting all the characters of the string into
the array, plus a zero to show the end, to be a sufficient
initialisation; one might not expect it to fill in all the other chars
as well.