Reputation: 61
I am just a beginner in C. I have written a code in C with a lot of memcpy functions. I want to convert the memcpy statements to memcpy_s.. I don't quite get the syntax to do this.
This is my code snippet:
signed char buffer[MAX];
unsigned char len;
const char *scenario = ConvertMap[identity];
len = strlen(scenario);
memcpy((void*)&buffer[0],scenario,len);
How do I convert the last line to memcpy_s? Is it like:
memcpy_s((void*)&buffer[0], sizeof(buffer), scenario, len)
?
Upvotes: 5
Views: 14372
Reputation: 141534
The code could be:
memcpy_s(buffer, sizeof(buffer), scenario, len)
although maybe you would want to use len+1
if you intend to copy a null-terminated string. (But in that case, use strcpy_s
).
Also you should allow for the error case: either design the rest of your code to behave properly if the memcpy failed (buffer will be nulled out), or check the return value and take action on failure.
Upvotes: 5