# std::string to char*

This topic is 4469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to turn a std::string that I used for my function arguement into a char * to use as an arguement to another function. I tried using .c_str, but that returns a const char * (say, why can't you assign a char * to the value of a const one?). How can I get around this problem?

##### Share on other sites
A simple (char*) cast should work, but why do you want to convert it to a non-const string? You shouldn't modify the data directly. If you need to modify the data inside of a function, pass a const std::string reference instead of a char pointer.

##### Share on other sites
Quote:
 Original post by Programmer16A simple (char*) cast should work, but why do you want to convert it to a non-const string? You shouldn't modify the data directly. If you need to modify the data inside of a function, pass a const std::string reference instead of a char pointer.

No, no, no. You should never cast away constness like that, even if you're 'sure' that your function never modifies the data. You are just undermining the security features of the language if you do that. If you really want to modify it, make a copy or else just have your function take a const char* as well.

##### Share on other sites
I can't say I'd recommend the doing any const casting trickery, since the internals of basic_string are mostly left up to the implementation. If you really must pass a non-const character pointer to a string, you can do something along these lines:
#include <iostream>#include <string>#include <vector>int main() {	// A string	std::string s("Testing");	// Copy s into a vector, then add a null terminator	std::vector <char> v(s.begin(), s.end());	v.push_back(char(0));	// And, finally, direct access to a pointer	char *v_str = &v[0];	// Let's print it out for some reason	std::cout << v_str << std::endl;	// Say you need to put it back into the string now (edit: I like this way better, actually)	s.assign(&v[0], v.size()-1);}

##### Share on other sites
I'm not entirely sure what you mean by that, so let me show you what I'm trying to do.

{
ilInit();
iluInit();
ilutRenderer(ILUT_OPENGL);
ilutInit();

return texture;
}

I'm wanting the std::string to do the concatenation. ILholder is a char *, which is what ilutGLLoadImage takes as a parameter. However, I can't get ILholder to be assigned loader. Also, I don't want to use const char *'s so that I can just make one, and not have to make a new one every time I call LoadGLTexture (which I do once a frame), because it's my understanding that'll drastically slow my program down.

##### Share on other sites
Okay, so (a) you need a string concatenation method and (b) you're just trying to interact with a function that should have its parameter declared const if it wasn't trying to be old-C compatible.

You can cheat in that case, I suppose :).
#include <sstream>// ...GLuint LoadGLTexture(const std::string &loader){  // ...  std::ostringstream sstr;  sstr << "Data/" << loader << ".bmp";  std::string str(sstr.str());    return ilutGLLoadImage(const_cast<char *>(str.c_str()));}

##### Share on other sites
People have been complaining about this problem with DevIL for years (including Muhammad Haggag in 2003). I'm surprised it hasn't been fixed yet.

##### Share on other sites
Quote:
 Also, I don't want to use const char *'s so that I can just make one, and not have to make a new one every time I call LoadGLTexture (which I do once a frame), because it's my understanding that'll drastically slow my program down.

This hasn't got to do with your original question, but if you call LoadGLTexture() each frame you will have a slowdown, but you can be sure that copying a string won't be the cause of it.

##### Share on other sites
Quote:
 Original post by PrototypeNo, no, no. You should never cast away constness like that, even if you're 'sure' that your function never modifies the data. You are just undermining the security features of the language if you do that.

Yea, I know that. I guess I should have added a 'but don't do this' message.
Quote:
 You shouldn't modify the data directly.

Oh wait, I did [razz]

Quote:
 Original post by PrototypeIf you really want to modify it, make a copy or else just have your function take a const char* as well.

Seems like I covered that as well.

And in the end, casting away constness was the answer [razz] (although using a C++ cast instead of a C cast.)

##### Share on other sites
Hello EmrldDrgn,

Actually it is very simple to do what you want. Just add one more line that makes use of a C Standard Library string copier function strcpy.

I did not see a declaration for ILholder in your original code so I added my own.

#include <cstring>

{
ilInit();
iluInit();
ilutRenderer(ILUT_OPENGL);
ilutInit();

delete []ILholder;

return texture;
}

strcpy is a function from the cstring header file. It basically does a copy from a char array to another char array. Its return value is the final char array pointer.

Hope this helps.

Regards,
Joshua

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Who's Online (See full list)

There are no registered users currently online

×