1

if i want to change a single char in a string i can do:

#include <iostream>
#include <string>
using namespace std;

int main() {
  string mystring = "Hello";
  mystring[0] = 'T';
  cout << mystring;
  return 0;
}

If i want to change i single char in a string using a different function:

#include <iostream>
#include <string>
using namespace std;

void changeStr(string* mystring);

int main() {
  string mystring = "Hello";
    changeStr(&mystring);
  cout << mystring;
  return 0;
}

void changeStr(string* mystring)
{
    mystring[0] = 'T';
}

Why isnt it working? The whole string gets changed to "T". Im new to programming and still got some problems with pointers / address. I know that a array of char (char[]) is a pointer to its first index. Does this apply to a string aswell? How can i fix it?

8
  • 6
    In c++ we prefer pass by reference instead of passing by pointer. I would write void changeStr(string & mystring) instead of void changeStr(string* mystring) Commented Aug 7, 2023 at 21:01
  • 4
    In your function you have a pointer to a string, not a string, So you should change your code to dereference the pointer (*mystring)[0] = 'T';. Or, like the comment above, you could change your function to use a reference. Commented Aug 7, 2023 at 21:02
  • 1
    I know that a array of char (char[]) is a pointer to its first index. Does this apply to a string aswell? No it does not, a std::string is an object, not an array or a pointer. Commented Aug 7, 2023 at 21:07
  • 1
    Typo. The first character of string* mystring is mystring[0][0] or (*mystring)[0]. You are assigning 'T' to the entire string. Commented Aug 7, 2023 at 21:16
  • 1
    General guideline for choosing between pointer and reference is this: If your function always expects a valid object, use a reference. Otherwise, nullptr should be considered as a possible value and you should use a pointer. Commented Aug 7, 2023 at 21:18

2 Answers 2

1

For starters there is no sense to pass an object of the type std::string to the function through a pointer to the object. You could pass it by reference. So the function could look like

void changeStr(string &mystring)
{
    mystring[0] = 'T';
}

Correspondingly the function is called like

changeStr(mystring);

As for your problem then at first you need to dereference the pointer and only after that to apply the subscript operator

void changeStr(string* mystring)
{
    ( *mystring )[0] = 'T';
}

Alternatively you could write

void changeStr(string* mystring)
{
    mystring->operator []( 0 ) = 'T';
}

Pay attention to that the postfix subscript operator has a higher precedence than the dereferencing operator.

As for the statement in your demonstration program within the function

mystring[0] = 'T';

then actually it assigns the character 'T' to the string using the assignment operator

basic_string& operator=(charT c);

instead of assigning only the first character of the string.

That is after this statement the string will be equal to "T".

It is the same if in main to write

mystring = 'T';

The expressions *mystring and mystring[0] where mystring has the pointer type std::string * are equivalent. So within the function you could even write

mystring[0][0] = 'T';

instead of

( *mystring )[0] = 'T';

Though as I pointed to early it is much better to pass the string by reference.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick answer. I tried "( *mystring )[0] = 'T';" without the brackets and got a compiler error. Why are does needed in order to work? What does it change?
@Bele The subscript operator has a higher precedence than teh dereferencing operator but you need at first to apply teh dereferncing operator.
0

you don't need to pass the pointer. Simply pass it by reference(reference will update the value) if you pass it by pointer then return the string instead of void.. better option .. pass it by reference

 #include <iostream>
 #include<string>
 using namespace std;
 void changeStr(string &mystring);

 int main() {
 string mystring = "Hello";
  changeStr(mystring);
 cout << mystring;
 return 0;
}

void changeStr(string &mystring)
{
  mystring[0] = 'T';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.