   having trouble converting a c++ string to cstring
  i'm using dev-c++ 4.9.9.2 and i'm having trouble converting a c++ string into a cstring (like the title says).  the reason i need to do this is because i'm using a string to hold the file name of a texture associated with a material.  however, to actually load the file i'm using DevIL's ilLoadImage() function which takes in a char* and not a string.  so i figured i would create a char* variable and assign it the value of object.filename.c_str().  however that isn't working and i get the error "invalid conversion from 'const char*' to 'char*'".  i really don't understand why it can't assign the value of the of object.filename.c_str() to the char* variable.  the char* variable isn't a const so it should be able to be modified.i have tried to do ilLoadImage(object.filename.c_str()) but i get the same error as above.does anyone know how to get around this?here is an example of what i'm trying to do:typedef struct{  ...  string filename;  ...}obj_type;int main(){  char* tempname;  obj_type object;    ... //load the filename to object.filename  tempname = object.filename.c_str();  ... //get DevIL ready to load an image  ilLoadImage(tempname);}
c_str() returns a " const char* "const char* tempname;will fix it.;Quote:Original post by supercat1i really don't understand why it can't assign the value of the of object.filename.c_str() to the char* variable.Because it returns (const char*).  This may be a temporary buffer, and not the "main" representation which is modified with normal string operations.  This buffer may also be shared between equivilant strings, so writing it would affect other strings when it shouldn't.  Thus, it's made const to avoid potential errors writing data you shouldn't - this is known as "const correctness".ilLoadImage should accept a (const char*) as it does not need to modify the string, and a (const char*) tempname should be used as well.  If you need a C style /writable/ string, you must use std::vector< char > - you can get a (char *) pointer in this case by writing (&my_vector_instance[0]).  This is only guaranteed to work with std::vector< T != bool > - other containers will not work, and the standard lifts some of the guarantees required for it to work with bool.   ;  PS."const char* var;" means that you promise not to change the values that the char* points to.   ;  The DevIL documentation is inconsistent as to whether ilLoadImage takes a const char * or a char *.  If passing object.filename.c_str() is failing like you say and you have an up-to-date copy of DevIL then you have two options:If you can be certain that ilLoadImage will not modify the passed character array then use a const_cast, i.e. ilLoadImage(const_cast< char * >(object.filename.c_str()));Otherwise, construct a temporary buffer and pass that, i.e.:std::vector< char > filename(object.filename.begin(), object.filename.end());filename.push_back('\0');ilLoadImage(&filename[0]);Σnigma   ;  thanks!isn't it true that if you define a variable as "const" anything then it can not be modified there after?  if so, then defining my temp char* variable (that must be changed) as a const is pretty much useless.enigma -- thanks i'll try the const_cast before i mess with the vector buffer stuff.  and you might be pleased to know that i am using the stl vector in my program.  i gave in because of the way the .3ds file format was setup made it much easier to just use the vector instead of fiddling with "new" (because it doesn't give the number of materials anywhere and therefore really messes with using "new" effectively).  :)update: const_cast worked.  thanks a bunch!   ;Quote:Original post by supercat1isn't it true that if you define a variable as "const" anything then it can not be modified there after?  if so, then defining my temp char* variable (that must be changed) as a const is pretty much useless.const with pointers has a few meanings.const char* var;means that the memory that var points to can not be modified (by var) but var can be pointed to different memory at any time.the other forms are:char* const var;const char* const var; see here for more details   ;  the AP was me but i forgot to log in.here is that link