codereviewanskquestions
codereviewanskquestions

Reputation: 13978

How do I get char * from vector<char>?

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..

Upvotes: 0

Views: 2044

Answers (3)

Deyili
Deyili

Reputation: 151

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

Upvotes: 4

Mark Ransom
Mark Ransom

Reputation: 308101

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.

Upvotes: 3

John Zwinck
John Zwinck

Reputation: 249093

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

&m_vbuffer[0]

Upvotes: 1

Related Questions