arnuld wrote:
>> On Mar 4, 4:51 pm, "Bo Persson" <b...@gmb.dk> wrote:
>> arnuld wrote:
>
>> At the time when C++ was designed, C didn't have the keyword const,
>> so all string literals were of the type char*. That made code like
>>
>> char* x = "Hello";
>>
>> valid. In C++ string literals have the type 'const char*', but this
>> old C code has got a special dispensation so that it will continue
>> to work anyway. Everywhere else it is an error to assign a pointer
>> to const to a pointer to non-const.
>
> what does last sentence mean:
>
> "a pointer to const to a pointer to non-const"
>
>
> this pointer to a char notation:
>
> char* x; or
> char *x;
These are entirely equivalent, the placement of the * doesn't matter.
What I meant is that if you have a pointer to const, like
const int* ci;
and a pointer to non-const
int* p;
it is not allowed to assign ci to p, as this would allow us using p to
change what is really const.
p = ci; // NOT allowed
But, with char* you are allowed to do this anyway
char* x = "Hello";
even though this assigns a 'const char*' to a 'char*'. There is nothing to
understand here, and nothing Bjarne is proud of, it just the way it has
always been.
Please stay with std::string, and save this for much later!
>> You have now found exactly the reason why many in this group
>> recommended "Accelerated C++", which introduces std::string on the
>> first page of chapter
>> 1. The subtle difficulties of pointers and arrays are postponed to
>> chapter 10, long after teaching about the simpler things like
>> classes, templates and overloaded functions.
>
> to me, i do not think pointers are difficult than Templates. i do not
> have any teacher/mentor who can teach me, except you folks. all of the
> colleges here teach a language named VC++ & they do not agree that
> "Linux is a good OS" and i am a UNIX man, that is the trouble.
But we have just seen that pointers ARE difficult, and that there are some
dark corners with very special rules. And that sometimes pointers and arrays
behave the same, and sometimes definitely not. This is the C part of C++,
that is not a good idea to try to learn first. It is not very useful,
really.
Bo Persson