| | |
Problems casting a const char* to char*
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 11
Reputation:
Solved Threads: 0
I am currently writing a C++ program that uses a external library and engine that is primarily based in C. I am attempting to load an image file and the function requires that the argument be passed as a char *szFileName.
I have the file address as a string and my attempt to convert is as follows:
std::string path("C:\\Doc.....")
then char *FileExt = path.c_str();
I continually get the error message saying that it is not possible to convert const char * to char *!
Any1 any ideas? Any help would be much appreciated!
oh and by the way ive tried casting it using static_cast
I have the file address as a string and my attempt to convert is as follows:
std::string path("C:\\Doc.....")
then char *FileExt = path.c_str();
I continually get the error message saying that it is not possible to convert const char * to char *!
Any1 any ideas? Any help would be much appreciated!
oh and by the way ive tried casting it using static_cast
The c_str() method of the std::string class returns a pointer to const char so that you can't modify the result. Unfortunately, there's no good solution, and you can thank the author of the library who didn't write const-correct code. Now, if you know that the function will not modify the string, you can use const_cast to allow the conversion:
If you're not sure whether the function will modify the string or not, you have no choice but to create a C-style string copy:
strcpy is declared in <cstring>.
>oh and by the way ive tried casting it using static_cast
static_cast doesn't remove qualifiers, which is a very good thing because you have to actively choose const_cast, and therefore can't do it by accident.
C++ Syntax (Toggle Plain Text)
char *FileExt = const_cast<char*> ( path.c_str() );
C++ Syntax (Toggle Plain Text)
char *FileExt = new char[path.size() + 1]; std::strcpy ( FileExt, path.c_str() );
>oh and by the way ive tried casting it using static_cast
static_cast doesn't remove qualifiers, which is a very good thing because you have to actively choose const_cast, and therefore can't do it by accident.
I'm here to prove you wrong.
![]() |
Similar Threads
- append char to char* (C++)
- I/O question. using vc++ read char by char (C++)
- OOP char [8] to char (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ How do you create multifile project
- Next Thread: backward string
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







