First things first, you should not cast the return value of malloc
(in C anyway) since it can hide errors.
Secondly, you never need to multiply by sizeof(char)
since it's always guaranteed to be one - doing so clogs up your code.
And, as to the actual question, you can use:
memcpy (temp, buff, BUFFSZ);
to copy the entire character array.
I'm assuming that's what you want because you make no mention of handling C "strings", only a character array.
If indeed you are handling C strings, strcpy
will work fine in this case, provided:
- you have room at the end of the buffer for the terminating zero-byte; and
- you've actually put the zero-byte in there.
For example, this little snippet works fine:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
// Two buffers.
char buff1[4];
char *buff2 = malloc (4);
if (buff2 == NULL) {
puts ("No memory!");
return 1;
}
// Populate first buffer.
buff2[0] = 'p';
buff2[1] = 'a';
buff2[2] = 'x';
buff2[3] = '\0';
// Transfer and print.
strcpy (buff1, buff2);
puts (buff1);
// Free and exit.
free (buff2);
return 0;
}