Copy unsigned char array
Asked Answered
B

1

13

What would be the best way to copy unsigned char array to another?

For example:

unsigned char q[1000];
unsigned char p[1000];

strcpy (q,&p);

The above code does not work, it gives me error saying "cannot convert parameter 1 from unsigned char [1000] to char *".

Bestir answered 22/12, 2010 at 7:56 Comment(2)
Are you sure you didn't mean to tag this question c++ and not c? There should be no problem using strcpy with unsigned char arrays (as long as they're null-terminated) in C. At worst an off-by-default warning.Severally
It does not matter if it is C or C++, using the &operator is always a bug here.Erlindaerline
G
27

As indicated by its name, strcpy works on C string (which a unsigned char array is not).

You should consider memcpy.

Gary answered 22/12, 2010 at 7:59 Comment(2)
You can cast an unsigned char* to char*.Passus
Can you provide a small example as I am having problems with copying an unsigned char variable to an unsigned char array using memcpy. passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [-Wint-conversion] Shipwreck

© 2022 - 2024 — McMap. All rights reserved.