I'm trying to convert a const unsigned char*
to char*
as well as make a copy. I have tried several variations of the code below but I usually get a memory exception. This function resides in an application written in C.
The below function is what I'm trying to create
//global variable
char* copiedValue;
void convertUnsignedChar(const unsigned char* message){
copiedValue = malloc(sizeof(message));
(void)memcpy(copiedValue, message, sizeof(message));
}
A large part of your problem is
sizeof(message)
which gives you the size of the pointer, not what it points to.Either pass the length to the function, or (if the data is a null-terminated string) use
strlen
to get the length (but don't forget to add one for the terminator!) or the above mentionedstrdup
function (if available).