views:

158

answers:

6

We have:

 std::string string_array[2];

 string_array[0] = "some data";

 string_array[1] = "some more data";

 char* cstring_array[2];

What is the most efficient way to copy data from string_array to cstring_array? Or pass string_array to the function, needed "const char* cstring_array[]"?

+3  A: 

Simply use:
string_array[0].c_str()
string_array[1].c_str()

See c_str():

const charT* c_str() const basic_string Returns a pointer to a null-terminated array of characters representing the string's contents.

Brian R. Bondy
Sorry, I've made a mistake. Function needs const char* array[]. ARRAY of const char* strings.
hoxnox
+4  A: 

Use string::c_str to get const pointer to a char.

Kirill V. Lyadvinsky
Sorry, I've made a mistake. Function needs const char* array[]. ARRAY of const char* strings.
hoxnox
+10  A: 

A function that takes a char const * is only written to accept a single string at a time, not an array of strings. To pass an std::string to such a function, just call the function with your_string.c_str() as the parameter.

Edit: for a function that takes an array of strings, the obvious choice (at least to me) would be to write a minimal front-end that lets you pass a vector<std::string>:

// The pre-existing function we want to call.
void func(char const *strings[], size_t num) { 
    for (size_t i=0;i<num; i++)
        std::cout << strings[i] << "\n";
}

// our overload that takes a vector<string>:
void func(std::vector<std::string> const &strings) { 
    std::vector<char const *> proxies(strings.size());

    for (int i=0; i<proxies.size(); i++)
        proxies[i] = strings[i].c_str();
    func(&proxies[0], proxies.size());
}
Jerry Coffin
Sorry, I've made a mistake. Function needs const char* array[]. ARRAY of const char* strings.
hoxnox
Awesome!!! Thanks!!!
hoxnox
+1  A: 

You can access a c-string version by passing string_array[1].c_str() to whatever function requires the string.

Be aware, however, that the pointer returned is dependent on the stl::string! If you take the pointer and then free the string, you will have an invalid pointer

cyberconte
Or even if you modify the string without freeing it. The buffer could be deallocated and replaced with a larger one.
Steven Sudit
Sorry, I've made a mistake. Function needs const char* array[]. ARRAY of const char* strings.
hoxnox
A: 

Two different questions, really. To pass to a function simply call c_str(). To copy over simply call strncpy() and remember what I said about passing to functions.

Keep in mind that std::string can contain '\0' without actually terminating. Under such conditions you'll only get part of the string. If you're trying to copy such data you'll need to use memcpy() instead.

Noah Roberts
Sorry, I've made a mistake. Function needs const char* array[]. ARRAY of const char* strings.
hoxnox
Then you'll be required to copy them, individually, into the array. There's no set of functions that could temporarily cast the one into the other.
Noah Roberts
Maybe there is a way to access memory, allocated in STL string? And some "magic" makes this memory looks like const char* array? I'm a deramer =))
hoxnox
@hoxnox - sure there is. c_str() or data(). What you want though is an array of strings turned into an array of character arrays and this is just not possible. Even if you tried it by some funky cast (which is easy) the distance between the internal buffers is not going to be sizeof(char*), which is what you need.
Noah Roberts
@Noah Roberts>just not possibleThat's what I'm afraid, but decided to ask ...
hoxnox
A: 

It looks like you're having some trouble with arrays. As Brian pointed out, array indices start at 0 not 1. Thus your two strings are string_array[0] and string_array[1], not string_array[1] and string_array[2].

Also, if your function takes a parameter const char* cstring_array, then your variable char* cstring_array[2] is probably not what you want. If you were to pass it in as is, you would essentially be passing in a char**, not a char*, as you can consider the name of an array to be equivalent to a pointer to its first element.

As you've presented your question, you could do something like the following (correcting for the indexing error, and assuming the function you mentioned is called myFunction):

...
cstring_array[0] = string_array[0].c_str( );
cstring_array[1] = string_array[1].c_str( );

myFunction (cstring_array[0]);
myFunction (cstring_array[1]);
...

However, a simpler equivalent that doesn't require cstring_array at all is:

...
myFunction (string_array[0].c_str( ));
myFunction (string_array[1].c_str( ));
...

Or, if your intention was to pass a single char array containing both strings (which is a possibility from my interpretation of the question), then what you really mean you want to do is you want to produce a single string being the concatenation of your original two:

...
std::string str = string_array[0] + string_array[1];

myFunction (str.c_str( ));
...

Hope that helps!

EDIT: given the comments that have appeared since I wrote this, I'd say the first example is close to what you need. I'd do the following (where N is the number of entries in either array):

...
for (int i = 0; i < N; i++)
    cstring_array[i] = string_array[i].c_str( );

myFunction (cstring_array);
...

This is the only approach possible. There is no standard function (AFAIK) that, given an array of string will give an array of char*. I suspect the reasons are that: - It's not something that needs to be done often, so there's little call for such a function to be widely available. - If there were such a function, it would most likely be very similar to the above anyway, in which case it's so trivial as to be unnecessary.

Basically, to answer your question, I don't think there is any more efficient method, and even if there were a standard library function to do this it would use this approach anyway.

Mac
>for (int i = 0; i < N; i++)> cstring_array[i] = string_array[i].c_str( );But, in that case I have to allocate memory to the cstring_array. Maybe there is a better way? Get access to memory allocated by STL string array, or smth... ?
hoxnox