Pete Becker <pe********@acm.org> wrote:
Frank Schmitt wrote: char *t="test";
printf(t);
This is asking for trouble - use
print("%s",t);
There's nothing wrong wtih printf(t).
Well, in this case there is nothing wrong with 'printf(t)'. If 't' is obtained
from somewhere, eg. from user input, it is not really a bright idea to use
't' as format string, however: it may contain formatting directives by
accident in which case the call to 'printf(t)' might easily yield funny
behavior.
Of course, this is not really related to the original problem which was
writing to string literal. The other problem not yet noted in this thread
was the fact that 'char' may be signed but 'toupper()' has to be called with
an 'unsigned char' or EOF (this is described in 7.4 paragraph 1 of the current
C standard; I don't have the C90 standard but I'm sure the same rule applies
there, too). Thus, the program should look something like this:
char t[] = "test";
for (char* it = t; *it; ++it)
*it = std::toupper(static_cast<unsigned char>(*it));
Of course, for "test" it is again unlikely to make any difference because
normally these characters happen to be represented by ASCII which uses all
positive values. However, in a real application the characters may include
non-ASCII character which may have negative values. Of course, in this case
there may be other problems as well: for example, the string may include the
German "sz" ligature for which there is no single character uppercase variant.
The uppercase variante of the "sz" ligature (which is just one character) has
two characters.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>