9uzman 9uzman - 1 year ago 184
C++ Question

C++ cast char * to unsigned char

I am working in a c++ project where I have to use < openssl/sha.h> and I am using in particular the SHA1 function. My problem is, that the function receives unsigned char[], and I need to get processed parameters passed as arguments to the program:

int main(int argc, char* argv[])

unsigned char message[] = argv[1];

/* program continues using message */


And the error I am getting is the following:

error: array initializer must be an initializer list or string literal
const unsigned char message[] = argv[1];

So I am not getting to cast appropiately the argument input to the 'message' variable, to make the appropiate call to SHA1 function.


Answer Source

An array cannot be initialized from a pointer. You should probably use an unsigned char * instead:

unsigned char *message = reinterpret_cast<unsigned char *>(argv[1]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download