Advertisement

Passing STL string as char *??

Started by June 07, 2000 05:18 AM
18 comments, last by Void 24 years, 6 months ago
A simple STL string problem I can''t seem to solve.. I want to pass the string variable to a function that accepts char *, like string StringToPass; StringToPass.reserve(100); // allocate memory GetString(StringToPass???) // <<-- How to pass to GetString function??? .. void GetString(char *) // accepts char * { } This must be unbelieveably easy, yet I don''t see a method in the SDK that can do that..8(.. Thanks..
You can use the string::c_str() member function to get a C-string.

Erik
Advertisement

No.. that wouldn''t work..

I need to pass in a char * as I need to pass the string by reference..

The c_str() is a const char *, which is good for messageboxes but not callback functions..

Anyone please??..

..
You want to pass the string by reference? Then you don''t want to use char* as parameter type, which is a pointer to a char, not a reference to a string. GetString would look like this, then:
void GetString(string& str){ // ...} 

GetString can then use str object and do whatever that''s needed.

Erik
The sad thing is the function taking the char * is not mine, it''s a windowsz function (actually D3DX, more specifically..
Why does it need to take it by reference? Is it to modify the existing char* string? If so, you will need to do something like:

char* buffer = new char[STLString.length()+1];
strcpy(buffer, STLString.c_str());
TheFunction(buffer);

Or is it just that you want the results from that callback stored into an STL string? If so, do it this way:

char buffer[SOME_LARGE_NUMBER];
TheFunction(buffer);
string STLString(buffer);
Advertisement
Thanks..

So it cannot be done without a char buffer..

I was hoping to avoid char array altogether and using the STL string for everything..Alas..

Thanks again..
I thought a string was just another container? I'm new to STL, but you may be able to do something with the stringbuf class...just a thought.

Are you sure you mean a callback function? A callback function is a function that you write, and then send its address to another program. The other program will then call your function with its own params.

If it is a C-style callback function, you might as well do the dreaded void* thing:


void some_function()
{
string my_string("Hello D3DX!");

void* pParam = static_cast&ltvoid*>(&my_string);

// fictitious function, but you get the point
set_up_callback_function(function_name, pParam);
}



Then just static_cast it back from a void* to a string*. Most callbacks just take void pointers anyway, so it's not that big a deal...or make the string a global and then you can still access it from inside your callback function.




- null_pointer
Sabre Multimedia


Edited by - null_pointer on June 9, 2000 8:05:33 AM
Well, you could also just use const cast to cast away the constness. Evil, but slightly less evil then casting it to a void *
something like

GetString( const_cast StringToPass.c_str() );

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight


the STL string is just another container. You don''t want to go around modifying its contents without using its supported methods tho.

If the API function isn''t going to modify the data the string contains, then you could get away with throwing away the const(ness) of the pointer returned by .c_str().

If the API function expects to modify the data, then it would be best if the data was copied into a different buffer for the call, then assigned to the STL string.

Don''t avoid using STL strings just because of a little unfriendliness with Windows..


__________________________________________

Yeah, sure... we are laughing WITH you ...

This topic is closed to new replies.

Advertisement