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 *".

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
user2829
  • 451
  • 1
  • 7
  • 21
  • 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. – R.. GitHub STOP HELPING ICE Dec 22 '10 at 08:03
  • 3
    It does not matter if it is C or C++, using the &operator is always a bug here. – kuszi Dec 22 '10 at 08:37

1 Answers1

26

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

You should consider memcpy.

icecrime
  • 74,451
  • 13
  • 99
  • 111
  • 1
    You can cast an unsigned char* to char*. – Engineer2021 Apr 03 '14 at 20:38
  • 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] ` – mLstudent33 Feb 09 '20 at 23:48