Mike <> writes:
> #include<stdio.h>
> main()
> {
> char s[]="Taiwan University.";
[...]
> }
[...]
At least one person has remarked on the fact that the code set the
array s to the value "Taiwan University." and then overwrites it.
In fact, given the empty square brackets, the initializer does two
things: it determines the initial value of s and it determines its
size (19 bytes in this case). Copying the string "Taiwan University."
into s is a bit wasteful, but it's a fairly concise way to say that
you want s to be big enough to hold that particular string value.
An alternate way to do the same thing is:
char s[sizeof "Taiwan University." + 1];
but it's easy to forget the "+ 1" (needed to allow for the terminating
'\0').
I'm resisting the temptation to propose adding a new feature to the
language so you can write:
char s[] != "Taiwan University."; /*

} */
to set the size without initializing the array.
(Of course, 19 bytes isn't big enough to make the program immune to
buffer overruns, but we've covered that.)
--
Keith Thompson (The_Other_Keith) <kst->
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"