0

I have a library function from which I should pass same data types to the main application .

Thus my question is - how do I pass a std::vector<char> to a C style function that expects char*. Here is what I have tried

// function to apply on char
void somefunction (char* c_buf, int* c_buf_len)
// main function 
typedef std::vector<char> Buf;
void (Buf& buf)
{

    // first convert `buf` to char and call function 
    somefunction(char_buf, char_buf_len)
    // Now convert buf to vector and do something with it..
}
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
  • 2
    Make it at least **look** like code. And it shouldn't be tagged with `c` – Eugene Sh. Feb 27 '15 at 17:02
  • I don't follow... convert `std::vector` to a single `char`? How exactly? Or to a `char *`? Does the `std::vector` contain `char`s? – wolfPack88 Feb 27 '15 at 17:03
  • One may explain what is the vector type, then one may manipulate `vector` instead of `char *`, as it is much safe IMHO – Hotted24 Feb 27 '15 at 17:05
  • what about &(Buf[0]) ? Should work,I guess – Severin Pappadeux Feb 27 '15 at 17:12
  • @RuggeroTurra: why did you change the C function signature to use `int` instead of `int*`? It is not uncommon for such a parameter to be used for both input and output, to specify the buffer initial size and then return the size written. That usage requires a pointer. – Remy Lebeau Feb 27 '15 at 17:17
  • I think changing the content of the question every minute does not contribute to valuable answers. – user0815 Feb 27 '15 at 17:23
  • I really do not know why this question has been marked as duplicate by Remy Lebeau . Is this complete answer ?http://stackoverflow.com/questions/3787074/convert-stdvector-to-array – Karthik Kondapaneni Feb 27 '15 at 18:15

4 Answers4

3

You cannot convert a vector to char. They're completely unrelated types. Good news is that you seem to need a pointer to a character buffer and a vector<char> is exactly that. You get the pointer to the begginning of its storage by taking the address of the first element (&buf[0]) or by saying buf.data(). buf.size() will give you (you guessed it!) the size of the buffer. That's all you need to call somefunction.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • 1
    I think you should mention that `&buf[0]` requires that the vector is not empty. – PaulMcKenzie Feb 27 '15 at 17:15
  • 1
    The `[]` operator does not perform bounds checking, so it won't matter what address it returns if `size()` is 0. The `at()` method, on the other hand, does perform bounds checking and will raise an exception. – Remy Lebeau Feb 27 '15 at 17:20
2

To pass a std::vector<char> to a function that expects a char *, you can

1) Pass the address of the first element in the vector. The only caveat is that using this method requires that the vector is not empty.

if (!buf.empty())
   somefunction(&buf[0], buf.size());

2) If using C++ 11 use the data() method for the vector. In C++ 11, using data guarantees that it will work with an empty vector.

   somefunction(buf.data(), buf.size());
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
0
int len = buf.size();
somefunction(&buf[0], &len);

You can do that because (From n2798 (draft of C++0x)):

23.2.6 Class template vector [vector]

1 A vector is a sequence container that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency. The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
0

Like this:

void (Buf& buf)
{
    int len = buf.size();
    somefunction(&buf[0], &len);
    // ...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770