| | |
converting std::string to const chars?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
My errors...
I'm using windows xp and Dev-cpp
God bless.
C++ Syntax (Toggle Plain Text)
#include <string> #include <iostream> void bull(char[]); int main() { std::string Yo_momma = "iz fat"; bull(Yo_momma.c_str()); //<--error here std::cin.get(); return 0; } void bull( char crap[] ) { std::cout << crap; }
My errors...
C++ Syntax (Toggle Plain Text)
In function `int main()': invalid conversion from `const char*' to `char*' initializing argument 1 of `void bull(char*)'
I'm using windows xp and Dev-cpp
God bless.
*Sigh* gotta love google.
This works apparently?
C++ Syntax (Toggle Plain Text)
char buf[255]; std::strcpy( buf, Yo_momma.c_str() ); bull(buf);
This works apparently?
•
•
Join Date: Jul 2005
Posts: 1,673
Reputation:
Solved Threads: 261
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().
•
•
•
•
void bull(char[]);
void bull( char crap[] )
{
std::cout << crap;
}
C++ Syntax (Toggle Plain Text)
void bull( const char[] ) and void bull( const char crap[] ) { std::cout << crap; }
I know this works, but I don't know if it is a best practise.
OUTPUT:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> void changeFirstLetter(char *str) { *str = 'J'; } int main() { std::string msg = "Hello, world!"; std::cout << msg << std::endl; changeFirstLetter(const_cast<char *>(msg.c_str())); std::cout << msg << std::endl; }
OUTPUT:
C++ Syntax (Toggle Plain Text)
Hello, world! Jello, world!
--Jessehk
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: C++ help
- Next Thread: help with finding minimum value
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






