Quote:
Originally Posted by smeezekitty
i use cin>> and cout<< all the time with char * with borland turbo C++ and g++
|
That probably means you were competent in C before you learned C++ (or maybe you had a really bad instructor for C++ that didn't know how to let go of C). I was competent in C long before C++ was invented, so I understand the viewpoint even if I don't agree with it.
Quote:
i see no reason to make it more complex to do C operations from withen C++
|
Beginners in C usually make mistakes with char* exactly like the OP in this thread made. Any language feature responsible for such a high number of beginner mistakes is a hint that something should be taught differently.
At some point, a C++ programmer (like a C programmer), needs to develop a good understanding of pointers. But I think it is better for a beginner to build up to that understanding though examples where the meaning and value of using a pointer is more obvious.
I think text is something a beginner should be able to work with even before they develop that good understanding of pointers.
Using char* when the conceptual object you are thinking about is a text string, not a character pointer, is a flaw in C and an unnecessary distraction in C++.
The object the programmer want to input is a text string, not a character pointer. Then I expect the object he wants to manipulate is a text string, not a character pointer. A C programmer must learn how to use char* when he intends string. A C++ programmer doesn't need to.
Quote:
Originally Posted by smeezekitty
instead of char *str use char str[14]
|
or
char str[BUFF];
That is a valid alternative for the trivial situation of the original example. I realized later that I may have been confusing matters when I ignored that post and said:
Quote:
Originally Posted by johnsfine
The first is wrong because sizeof(str) will always be the size of the pointer, not the size of the buffer pointed to.
|
In fact, when str isn't a pointer, sizeof(str) isn't "always" the size of a pointer. So one of the advantages of declaring str the way smeezekitty suggested is that sizeof(str) will have the meaning a beginner expects.