The pointer is not read-only. (the string data itself is, but a pointer pointing to it can be modified freely) However, assigning a character to a pointer doesn't do what you expect.
In general, the only thing you can assign to a point is an address. You can't assign values, only the address of values.
String literals (like "hello world") are the one exception, because strings are special. If you assign one of those to a pointer, you get a pointer to that string. But in general, you assign addresses to pointers.
The other point is that characters in C++ are integral datatypes. They can be treated as integers with no casting required.
I can do int i = 'W'
and the compiler won't complain.
So what happens if you assign 'W' to a pointer? It takes 'W' as an integer value, and assumes that this is an address. 'W' has the ASCII value 127, so you are effectively setting your pointer to point to address 127, which doesn't make sense.
I don't see how that has much to do with your code though.
The problem there seems to be that temp
doesn't point to valid data. You declare a pointer, which points to some undefined address. And then you say "wherever it points to, I want to write the value that s
points to. The following should work somewhat better:
char temp; // not a pointer. We want a character to store our temporary value in
while (s <= e) {
temp = *s; // note, no * on temp.
*s = *e;
*e = temp; // note, no * on temp.
e--;
s++;
}
However, if str
points to a string literal, like "hello world", then this won't be legal, because the string data itself is read-only. The compiler may not enforce it, but then you've ventured into undefined-behavior land. If you want to modify the string, copy it into a local buffer, as one of the other answers showed.
You seem a bit confused about the semantics of pointers. Assigning an address (or something that can be converted to an address, like an integer) to a pointer makes the pointer point to that address. It doesn't modify the pointed-to data.
Declaring a pointer doesn't mean that it will point to anything meaningful. If you want to store a char, declare a char variable. A pointer doesn't store data, it just points to data allocated elsewhere.
edit
Comments and fixes to your updated code:
void something(const char * str) { // let the function take a pointer to a non-modifiable string, so add the const. Now it's clear that we're not allowed to modify the string itself, so we have to make a copy.
char *s = new char[strlen(str) + 1]; // Since the original string is const, we have to allocate a copy if we want to modify it - in C, you'd use malloc(strlen(str)) instead
strcpy(s, str);
char *e = s; // make e point to the start of the copied string (don't allocate two copies, since e and s are supposed to work on the same string
while (*e != '\0') { // add braces so it's clear where the loop starts and ends.
e++;
}
e--;
while (s <= e) { // the loop condition wouldn't work if s and e pointed to separate copies of the string
cout << *e; // why? I thought you just wanted to reverse the string in memory. Alternatively, if you just want to print out the string reversed, you don't need to do most of the rest of the loop body. In C, you'd use printf instead of *e
char temp = *s; // might as well declare the temp variable when you need it, and not before
*s = *e;
*e = temp;
e--;
s++;
}
}
Just for reference, and in response to the comments about C vs C++, here's how I'd write a function to reverse a string in C++:
std::string revert_string(const std::string& str) {
return std::string(str.rbegin(), str.rend());
}
Or to revert the string in-place:
std::string revert_string(const std::string& str) {
std::reverse(str.begin(), str.end());
}