views:

262

answers:

1

So, I have two types at the moment:

const unsigned char* unencrypted_data_char;
string unencrypted_data;

I'm attempting to perform a simple conversion of data from one to the other (string -> const unsigned char*)

As a result, I have the following:

strcpy((unencrypted_data_char),(unencrypted_data.c_str()));

However, I'm receiving the error:

error C2664: 'strcpy' : cannot convert parameter 1 from 'const unsigned char *' to 'char *'

Any advise? I thought using reinterpret_cast would help, but it doesn't seem to make a difference.

+7  A: 

You can't write to a const char *, because each char pointed to is const.

strcpy writes to the first argument. Hence the error.

Don't make unencrypted_data_char const, if you plan on writing to it (and make sure you've allocated enough space for it!)

And beware of strcpy's limitations. Make sure you know how big your buffer needs to be in advance, because strcpy doesn't stop 'til it gets enough :)

Alex
Unfortunately, its not my choice to make it constant or not. The parameters of the AES encryption function that I'm passing unencrypted_data to require that it is in the form of "const unsigned char", hence my problem.
BSchlinker
BSchlinker, that's not a problem. `unsigned char*` can be implicitly converted to `const unsigned char*`.
Matthew Flaschen
No, no -- If a function takes something like `void f(const char *)`, it's just saying that *it* won't write to it! It doesn't stop you from giving it a mutable buffer.
Alex
Like Alex said: Avoid using strcpy! Maybe using strncpy is an option.And: If you are using a mutable buffer for the AES function and then strcpy to copy its contents to another mutable buffer. Why don't you give the latter to the AES function directly?
Tilo Prütz