On Sun, Mar 23, 2008 at 11:03 AM, N i v a s <[EMAIL PROTECTED]> wrote:

>   char arr[] = "ABCDEF";
>   char *ptr = "STUVWXYZ";
>
>   arr[2] = 'X';     //No probs
>   ptr[2] = 'K';   //Error
>
>   why I'm not able to change the second array contents using ptr?

You should not attempt to modify a string literal -- you are creating
string constants and then trying to modify them. Technically, it's
undefined (from C++ standard section 2.13.4):

"Whether all string literals are distinct (that is, are stored in
non-overlapping objects) is implementation-defined. The effect of
attempting to modify a string literal is undefined."

The correct way to do what you want to do is to allocate memory for
char *ptr and then copy the string into it. Of course, if you are
using C++, you should be using the ANSI string class instead of
fooling around with char *

-- Brett
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
 If I were to divulge it, it would overturn the world."
 -- Jelaleddin Rumi

Reply via email to