I have a vector of chars:
vector<char> bytesv;
I push 1024 chars to this vector in a loop (not important to the context) using push_back(char):
bytesv.push_back(c);
I know this vector has an exact value of 1024. It indeeds print 1024
when doing the following:
cout << bytesv.size() << "\n";
What I am trying to do: I need to transform this vector into a char array (char[]) of the same length and elements as the vector. I do the following:
char* bytes = &bytesv[0];
The problem: But when I print the size of this array, it prints 4
, so the size is not what I expected:
cout << sizeof(bytes) << "\n";
Full code:
vector<char> bytesv;
for (char c : charr) { // Not important, there are 1024 chars in this array
bytesv.push_back(c);
}
cout << bytesv.size() << "\n";
char* bytes = &bytesv[0];
cout << sizeof(bytes) << "\n";
Prints:
1024
4
This obviously has to do with the fact that bytes
is actually a char*
, not really an array AFAIK.
The question: How can I safely transfer all the vector's contents into an array, then?