0

I have declared a vector like this

vector<char> vbuffer;

and used it as a receive buffer like this..

recv = read(m_fd, &m_vbuffer[totalRecv], SIZE_OF_BUFFER);

It seems like working and now I want to get the raw char data for parsing..

If I have defined a function like this..

char* getData(){
//return char data from the vector

}

How would I fill inside of the function? Thanks in advance..

codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167

3 Answers3

4

in c++0x, simply do m_vbuffer.data()

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
Deyili
  • 151
  • 4
3

As long as you don't resize the vector in any way, you can use &vbuffer[0] as a pointer to the array. There are many operations that will invalidate pointers to a vector though, make sure you don't call any of them while you have a pointer in use.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

This will give you a pointer to the first element in the vector, so you can use it like an array:

&m_vbuffer[0]
John Zwinck
  • 239,568
  • 38
  • 324
  • 436