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.