u_char a[3] = {'a', 'b', 'c'};
functionThatRequiresUChar(a);
But what if you don't know how big the u_char will be at compile time?
I tried doing something like this:
int len;
cin >> len;
u_char a[len];
But the compiler won't do that as it says it can't allocate memory of
an unknown size.
I tried doing this:
int len;
cin >> len;
u_char *a;
a = new u_char[len];
functionThatRequiresUChar(a);
And it compiles. However, is this the right way to do it?
Will the function be able to tell I have made the u_char doing that
(apparently) non-standard way?
Or do I have to do it like
functionThatRequiresUChar(*a);
?
Thanks for any help.
Use std::vector.
#include <vector>
std::vector<u_char> a(len);
functionThatRequiresUChar(&a[0]);
--
Richard Herring
int len;
cin >> len;
char* blah = "asdf";
std::vector<u_char> a(len);
for (int i = 0; i < len; i++) {
&a[0][i] = blah[i];
}
a = u_char[3] = {'a', 'b', 'c'};
functionBlah(a);
And with std::vector, when I try to pass it the entire u_char array
instead of one character from it, it says 'type cast' : cannot convert
from 'std::vector<_Ty>' to 'u_char'.
Use the constructor that takes two iterators:
std::vector<u_char> a(blah, blah+strlen(blah));
--
Richard Herring
Post the _actual_ code you tried to compile, and the error message.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
--
Richard Herring
char* source = "asdf";
u_char* dest = new u_char[len];
for (int i = 0; i < len; i++) {
dest[i] = source[i];
}
Is it better to use a vector?
Yes. Using std::vector means you don't have to worry about the delete[]
which is missing from your code above.
std::vector<char> dest(source, source+strlen(source));
--
Richard Herring