| | |
converting std::string to const chars?
![]() |
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,671
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 |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix matrix3d memory multiple news node output parameter pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings temperature template test text text-file tree url variable vector video visualization win32 windows winsock word wordfrequency wxwidgets






