[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Language War (Re: "C" Manual)



dman wrote:
> 
> On Thu, Jan 03, 2002 at 09:39:09PM -0600, Gary Turner wrote:
> | On Thu, 03 Jan 2002 17:34:00 -0600 (CST), Richard Cobbe wrote:
...
> | >    char str[] = { 'b', 'a', 'd', ' ', 's', 't', 'r', 'i', 'n', 'g' };
> | >    // note the lack of a terminating '\0'!
> | >    cout << str;
...
> | Neophyte that I am, I feel like I'm bringing a knife to this gunfight.
> | This example looks to be a cheat, in that you've defined an array and
> | then treated it as a string (legal).  Had you defined a string, it would
> | be null terminated and index addressable.
> 
> He did define a string.  In C++ there are 3 ways of defining a string
> (in C there are 2).  There is "char[]", "char*" and "std::string".
> The latter is the best way because it provides the most protection.

  no, he did not define a string, he defined an array of chars, which is
often used as string, string manipulating functions in standard
libraries assume that a pointer to char (or char array) will be ended by
'\0' but a char array that does not end with zero is just as good char
array as any other (as far as its type goes).

  therefore when the function fails (the one that assumes '\0' at the
end), it does not fail because of type problem but because of the wrong
value.

  there is no such thing as string type in c or c++ language (there is a
string in c++ standard libraries).

	erik



Reply to: