char * vs. string


R. Clayton (rclayton@monmouth.edu)
Tue, 11 Jul 2000 20:22:20 -0400 (EDT)


I've been noticing that many people are using the char * data type to represent
strings in their programs. char * is nasty, primitive data type, and using it
to represent strings in C++ (and in c) is a difficult, dangerous task,
particularly for those of you without much programming experience and
especially for those of you without much C-C++ programming experience.

Almost all the char * handling code I've seen commits serious programming
errors, and starting with the list assignment I'm going to start writing test
cases that pushes your code into the deep, dark holes your digging for yourself
by using char * to represent strings.

I strongly encourage you to replace almost all uses of char * in your code with
the C++ string class (you won't be able to eliminate all occurrences of char *
because, for example, the command-line arguments come in as char * strings).
Don't think you can patch your char * code to make it work: tracing program
crashes back to char * abuse is a tedious and frustrating process (and, as some
of you are finding out, different systems react to char * abuse in different
ways), and the char * fixes I've seen were disgusting when they weren't
ineffectual.

You can find information on the string data type in Nyhoff, Section 3.4.



This archive was generated by hypermail 2.0b3 on Fri Aug 11 2000 - 15:25:05 EDT