"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:dp**********@osf1.gmu.edu...
Dear All,
I hear a lot that cstring cannot be changed. But my code:
/************************************************** ***********
* Testing cstring(a pointer)
* ************************************************** ********/
#include <iostream>
using namespace std;
int main()
{
char shortsTring[] = "abc";
shortsTring[2] = 'X';
cout << shortsTring << endl; //print out abX !!!
return 0;
}
So why many people are saying cstring cannot be changed? Could you help
me? Thank you very much.
constant "cstring" (c style strings) can not (should not) be changed. It is
fine to change non constant c style arrays as you've seen.
Whoever told you that all c style strings can not be changed was simply
wrong. A C style string is simply an array of characters which is null (0)
terminated. Nothing more, nothing less. You can change the data just as
you can change any array data.
Note the difference between:
char* shortsTring = "abc"; // shortsTring points to constant data
and
char shortsTring[] = "abc"; // shortsTring is an array of non constant data