vector::pointer confusion

  • Thread starter Kristofer Pettijohn
  • Start date
K

Kristofer Pettijohn

I'm hoping someone can help me understand the vector::pointer..

What is the correct way to go about going from:

std::vector<char> myCharVector(30);

to a char* ? (bad example, but char *myCharArray = _______ .... )

Thanks!
 
G

Gernot Frisch

Newsbeitrag
I'm hoping someone can help me understand the vector::pointer..

What is the correct way to go about going from:

std::vector<char> myCharVector(30);

to a char* ?


char* Mychars = &myCharvector[0];
 
R

Ron Natalie

Kristofer Pettijohn said:
I'm hoping someone can help me understand the vector::pointer..

What is the correct way to go about going from:

std::vector<char> myCharVector(30);

to a char* ? (bad example, but char *myCharArray = _______ .... )

If myCharVector is going to stay around (and you're not going to add anything
to it that would cause a reallocation):

char* myCharArray = &myCharVector[0];

Otherwise, you're going to have to allocate a 30 element char array and copy
the contents into it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top