converting std::string to const chars?

Reply

Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

converting std::string to const chars?

 
0
  #1
May 9th, 2006
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.

  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...

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: converting std::string to const chars?

 
0
  #2
May 9th, 2006
*Sigh* gotta love google.

  1. char buf[255];
  2.  
  3. std::strcpy( buf, Yo_momma.c_str() );
  4. bull(buf);

This works apparently?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: converting std::string to const chars?

 
0
  #3
May 9th, 2006
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().
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: converting std::string to const chars?

 
1
  #4
May 14th, 2006
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:
  1. void bull( const char[] )
  2. and
  3. void bull( const char crap[] )
  4. {
  5. std::cout << crap;
  6. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 19
Reputation: Jessehk is an unknown quantity at this point 
Solved Threads: 2
Jessehk's Avatar
Jessehk Jessehk is offline Offline
Newbie Poster

Re: converting std::string to const chars?

 
0
  #5
May 14th, 2006
I know this works, but I don't know if it is a best practise.

  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:
  1. Hello, world!
  2. Jello, world!
--Jessehk
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC