944,066 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14624
  • C++ RSS
May 9th, 2006
0

converting std::string to const chars?

Expand Post »
Hullo, I need help here I can't get this to work. I'm not sure if the code is correct. Any help would be most appreciated. I basically wanna convert a std::string to a const char array.

C++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. #include <iostream>
  3.  
  4. void bull(char[]);
  5.  
  6. int main()
  7. {
  8. std::string Yo_momma = "iz fat";
  9.  
  10. bull(Yo_momma.c_str()); //<--error here
  11.  
  12. std::cin.get();
  13. return 0;
  14. }
  15.  
  16. void bull( char crap[] )
  17. {
  18. std::cout << crap;
  19. }

My errors...

C++ Syntax (Toggle Plain Text)
  1. In function `int main()':
  2. invalid conversion from `const char*' to `char*'
  3. initializing argument 1 of `void bull(char*)'

I'm using windows xp and Dev-cpp

God bless.
Similar Threads
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 9th, 2006
0

Re: converting std::string to const chars?

*Sigh* gotta love google.

C++ Syntax (Toggle Plain Text)
  1. char buf[255];
  2.  
  3. std::strcpy( buf, Yo_momma.c_str() );
  4. bull(buf);

This works apparently?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 9th, 2006
0

Re: converting std::string to const chars?

c_str() returns a const char []. bull() takes a char [] as a parameter. That means that any changes made to char[] in bull() should be valid changes in c_str(). However since c_str() returns a const char [] it can't be changed. Therefore the compiler throws an error. Hence, you have to copy c_str() to a char [] before passing it to bull(). You could also try to temporarily ignore the constantness of c_str() when passing it to bull() by using the using the C++ syntax const_cast before the call c_str().
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
May 14th, 2006
1

Re: converting std::string to const chars?

Quote ...
void bull(char[]);


void bull( char crap[] )
{
std::cout << crap;
}
Assuming you won't in fact attempt to change 'crap' inside the 'bull' function, simply change the above to:
C++ Syntax (Toggle Plain Text)
  1. void bull( const char[] )
  2. and
  3. void bull( const char crap[] )
  4. {
  5. std::cout << crap;
  6. }
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 14th, 2006
0

Re: converting std::string to const chars?

I know this works, but I don't know if it is a best practise.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. void changeFirstLetter(char *str) {
  5. *str = 'J';
  6. }
  7.  
  8. int main() {
  9. std::string msg = "Hello, world!";
  10.  
  11. std::cout << msg << std::endl;
  12. changeFirstLetter(const_cast<char *>(msg.c_str()));
  13. std::cout << msg << std::endl;
  14. }

OUTPUT:
C++ Syntax (Toggle Plain Text)
  1. Hello, world!
  2. Jello, world!
Reputation Points: 33
Solved Threads: 2
Newbie Poster
Jessehk is offline Offline
19 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ help
Next Thread in C++ Forum Timeline: help with finding minimum value





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC