0

I have function definition lke below

void ConvertString(std::string &str)
{
size_t pos = 0;
    while ((pos = str.find("&", pos)) != std::string::npos) {
         str.replace(pos, 1, "and");
         pos += 3;
    }
}

Purpose of this function is to find & and replace it with and. function execution in fine. I written this for all generalised string at one instance I am calling this in following way

char mystr[80] = "ThisIsSample&String";
ConvertString((std::string)mystr);
printf(mystr);

In above call I am expecting console should be printed with new modified string with "and". But some of string modification is not working , any error in function?

2
  • Why not just use std::string? std::string mystr = "ThisIsSample&String"; Commented Aug 27, 2014 at 6:53
  • You're using Visual C++ right? Because this shouldn't compile. Commented Aug 27, 2014 at 6:53

5 Answers 5

4

This code:

char mystr[80] = "ThisIsSample&String";
ConvertString((std::string)mystr);
printf(mystr);

… creates a temporary string object and passes that as argument.

Since the formal argument type is by reference to non-const, this should not compile, but Visual C++ supports it as a language extension (for class types only, IIRC).

Instead do like

string s = "Blah & blah";
ConvertString( s );
cout << s << endl;

By the way, C style casts are in general an invitation to bugs, because the basic nature of such a cast can change very silently from e.g. const_cast to reinterpret_cast when the code is maintained.

It's safe enough in the hands of an experienced programmer, like a power tool such as a chain saw can be safe in the hands of an experienced woodsman, but it's not a thing that a novice should use just to save a little work.

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

Comments

2

It's because you create a temporary std::string object (whose initial content is the content of the array mystr), and pass that temporary object by reference to the function. This temporary object is then destructed when the call id done.

1 Comment

It should not even compile
1

Did you read some documentation of std::string and of printf?

You need

std::string mystr = "ThisIsSample&String";
ConvertString(mystr);
printf(mystr.c_str());

You obviously want to pass by reference a string variable (technically an l-value) to your ConvertString

Comments

1

I believe your problem is that you cast char array to string.

ConvertString((std::string)mystr);

this line creates a new variable of type std::string and passes it by reference. What you want is to convert it this way:

std::string convertedStr = (std::string)mystr;
ConvertString(convertedStr);
printf(convertedStr.c_str());

I am not very well aware of C++ pointer and reference syntax, but it's similar to this

3 Comments

It would look less weird to say std::string convertedStr = mystr;
@juanchopanza (std::string)mystr makes type conversion more explicit in my opinion
If you want to be explicit you should use std::string convertedStr = std::string(mystr);. The C-style cast syntax looks pretty strange in C++.
0

what your are doing is not correct! you cannot should not convert a char* to a std::string with a cstyle-cast. what you should do is more like:

std::string mystr( "ThisIsSample&String" );
ConvertString(mystr);

edit: thx for -reputation... this code isn't even compiling... http://ideone.com/bCsmgf

3 Comments

Yes, you can. It will result in a temporary std::string object, constructed from the char*.
@Benjamin Lindley: thx for your answer. But IMHO it is not a question if you can, it is a question about if it's correct.... my sentense "you cannot" is not about that your are not able to do so (you are able to do so many nasty things with c++), it should say: You cannot, because it will not work this way... thx anyway
But the c-style cast is not the issue. The issue is passing an rvalue to a function taking a non-const lvalue reference. The C-style cast is just bad style.

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.