Trouble overwriting data in c-style strings using pointers

As part of a class project, we were tasked with creating a number of functions using both subscript notation and pointer arithmetic to make a set of driver code function. The driver code included three character arrays, one declared as an array, and the other two declared using pointers:

1
2
3
char s1[ 100 ];
char* s2 = "education";
char* s3 = "school";


I was able to manipulate the first string without any problem, copying the other strings into it, truncating, etc. However, the other two would not allow me to edit them in any way, and it was only when I changed how they were declared that I could make changes to those strings. Regarding the changes I wanted to make to those two strings, I just wanted to truncate the string after the middle character and return the address of the middle character.

What am I missing in regards to how c-style strings behave when they are declared using pointers, in particular how to manipulate the data stored in those memory locations? When I showed my instructor what I was trying to accomplish, he said that my code should work as written and he wasn't sure where the problem was.

I should probably also mention that the function I was writing to locate and return the address of the middle character of a string was not part of the original class project. I simply included it in that program to test the function since the program already had a number of strings available to work with.
Last edited on
A string literal is a constant array, the declarations above should not even compile on C++.
A correct declaration would be:
const char* s2 = "education";
Those strings are read-only thus cannot be modified.
Thank you for the quick response. I suspected that the strings in question (s2 and s3) were somehow being treated as constants, but I wasn't able to find anything in the textbook or online that gave me 100% confirmation of that. To make sure I understand correctly, the fact that they were declared using a literal string value ("education") forces those strings to be treated as constants? Is there a way they can be declared using a pointer where they would not be constants?

I am curious about why the existing declarations shouldn't compile. I have been using MS Visual Studio while working on this project and it hasn't complained about either of those declaration lines. Would it be automatically adding the "const" during compilation, or is this just something that VS handles behind the scenes?
C++ says that the type of a "string literal" is an array of const char.
char* s2 = "education"; would be valid in C since C doesn't require that two pointers must be of the same type to be assigned.
C++ requires this but the bad habit of the char* declarations for string literal remained.
This causes some implementations to accept that declaration even if the pointer still points to an array of constant characters.

Try to disable language extensions / enable warnings and see if it says anything.
g++ would give this warning: deprecated conversion from string constant to ‘char*’
Thank you again. You've helped me clarify some more of how C-style strings work in C++. It seems like whenever I think I have a grasp on something, there are always more intricacies in the language just waiting to trip me up. Of course, that's what makes this so interesting.
Topic archived. No new replies allowed.