why here the const is droped? can you explain it more specific here?

this is a feature of C and C++ to cater for old code.

This is the part of the agreement the compiler has to follow or to allow. If it makes you happy, think of it as a feature which the standard mandates.

char *str = "Hello";
the "const drop" Is it the feature which standard mandates or its an addition??

Either you're incredibly dense, or you're putting way too much thought into something simple. Given char *str = "Hello"; , "Hello" is const, str is not. The assignment is allowed, but you can't use str to modify "Hello". That's as deep as it goes, so stop digging.

commented: Wow, I wish I could explain stuff that simply. +11

Correct me here if i'm wrong..

Originally Posted by Salem
this is a feature of C and C++ to cater for old code.

You mean some time ago people needed to use strings somehow so they said that the compiler should allow assign address of const string to a pointer, then they did it as specific implementation. The language doesn't mandate it??

so here

char *ptr = "String";

the compiler allows assign the address of the constant string to the non constant pointer(its implementation specific).

so from now the compiler thinks that all ok.

But the string actually saved at constant memory, so when we are trying to modify it (*ptr = 'H';) the program crashes?

Thats it??

>Thats it??
Close enough. How many times do you need to repeat the same thing to get it?

Can you just tell me if its correct so that i can be sure?
If you say " Close Enough" so maybe you just tell me what is correct once for good??

>so maybe you just tell me what is correct once for good??
I did. Everyone did. Given char *ptr = "String"; , "String" is const, ptr is not, the assignment is legal, but you can't use ptr to modify "String". I can't put it any more simply.

Oh sorry you maybe confused with my questions because of my poor english.

Look all i want to know is

char *ptr = "String";

Why here the assignment is legal?

its should be ilegal - converting array of 6 const char to *char.

Salem told me:

this is a feature of C and C++ to cater for old code.

So i asked you what he meant.

Now you are not confused so maybe you can tell me what he meant.

Is he meant that
some time ago people needed to use strings somehow so they said that the compiler should allow assign address of const string to a pointer, then they did it as specific implementation. The standard/language doesn't mandate it??

Is it what he meant??
If no, can you tell me what he meant?? ;)

Because my english is not the best. ;)

can you tell me what he meant here:

Originally Posted by Salem
this is a feature of C and C++ to cater for old code.

Thats all i ask really.

>Why here the assignment is legal?
Because string literals weren't always const. Thus, a lot of existing code assigns a string literal to a non-const pointer. So rather than break all of that code, the standard allows this particular assignment as an exception.

look if i understood it good.

you say that long time ago string literals were not constants. so long time ago its was allowed to assign string literal to a a pointer because string literal long time ago was type of 'arrays of char' and not 'arrays of const char'.

and today when string literals are constants,
people are used to old code where strings literal are not constants.

so the new compilers are treating strings literals like in old times??

when i mean old times i mean that its not constants.


Thats it??

No no no my post 40 is not right.

tell me if this one correct.

Long time ago strings were not constants.
so long time ago it was allowed to assign string literal to a pointer because strings literal were 'arrays of char' and not 'arrays of const char'

but today strings literal are constant.
but today people used to old code where strings literal are not constants.

so the new compilers allow this:
char *ptr = "String";

as an exception.

but then when we are trying to modify it here:

*ptr = 'X';

but then its depends on our option
/GF OR /Gf

one of them saving the string in constant memory so we cant modify then we are getting crash at RunTime because of this ( *ptr = 'X';).

and one of them saving strings in non constant memory so we can modify it.

Is it correct???

>but then its depends on our option
>/GF OR /Gf
Too specific. Just because your compiler has this switch doesn't mean mine does too. Up to this point you're long winded, but correct. I'm worried what will happen when you come across something truly complicated. It'll take us years to explain it to you. :icon_rolleyes:

Okay listen i got it all, i very appreciate your help and all others here that tried to help me ;)

Now after i understood every thing let me test my self, because i'm doubting about this stuff and all because i'm 14 and i'm not suppose to learn programing in this age but i cant its like drugs now lol ;)

Now i will tell all about this stuff and you just tell me if i'm right, this is the last time.

Think about it as a summary.


Long time ago Literal Strings were not constants.
But today in our days they are constants.
However people used to assign literal strings to pointers from old code.

Therefore the new compilers support this way when we are assigning a literal string to a pointer , the string is losing its constant'ness.

So here:

char * pointer = "String Literal";
* pointer = 'x';

the compiler takes it as an exception but only with char pointers.
What is does here is:
the string literal losing its constant'ness therefore the pointer is allowed to point at the first Letter of this string.
but the compiler doesnt know that "String Literal" is constant because when we are assigning string literal to a pointer the compiler takes it as an exception and the string losing its constant'ness therefore we are allowed to modify the string at the compile time because the compiler doesnt know that its constant.
so its compiles.

but in runtime when we are trying to modify the string:
*pointer = 'x';

we are getting a crash because this string literal was saved at a constant memory so when we are trying to modify it,
its doesnt give us access and therefore the program crashes.
or if we choose write able strings at the compiler options, its will save them at read and write memory then we can modify it but its a bad idea because constants need to be constants..

so the result is that:
When the compiler sees a string literal assignned to a pointer,
the string losing its constant ness and then its can be legal.
but then its depends on our compiler option that we choosed we can choose write able strings so the compiler will save them in regular memory, or if we choose unwrite able strings the compiler will save them in constant memory this option is specific.
But the options doesnt touches the assigment with string to a pointer because the compiler automaticly forgeting that the string is constant and its doesnt matter where its saves the string even at read memory and even at write/read anyway the compiler will forget that its constant..

Its the last time i ask.

Is it correct?

>Is it correct?
If I say yes, will you believe me and stop trying to repeat your explanation over and over?

commented: It's a marathon isn't it. +6

Its the last time i asking :)

please can you read it and say me if its correct so i will know for sure?

I have to admit this thread is one of its kind. We don't get to see such things everyday. I must admit that one more question and you would force Narue into giving you an infraction. I can imagine her banging her head on her screen at this very moment. ;-)

For the fifth time, YES. You're correct. Geez. Most people are wrong and don't care, but you're right and lack the confidence to believe it.

Thanks All Thanks!!!!

I realy realy realy appreciate this 10000 replys help !!

Here's a tip for my future sanity: When first learning, you don't need all of the details. It's enough to know the rule even if you don't know why or how. That stuff comes later when you want to master your knowledge of the language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.