Reputation: 1026
Please, tell me, what is the correct way of copying allocated char array to "normal" char array? I have attempted to do the following, but it fails :
char * buf = (char *) malloc (BUFSIZ * sizeof(char));
// filling up the allocated array with stuff...
char temp[BUFSIZ];
strcpy(temp, buf); // strcpy doesn't work
Upvotes: 0
Views: 3355
Reputation: 881633
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:
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;
}
Upvotes: 1
Reputation: 145849
strcpy
is a function that copy a string.
A string is a sequence of character terminated by and including the null character.
char *buf = malloc(BUFSIZ);
This malloc
call allocates an array BUFSIZ of char
but this is not a string.
To copy an array use the memcpy function:
memcpy(temp, buf, BUFSIZ);
If your array holds a string (a sequence of characters terminated by a null character), you can then use strcpy
to copy it.
Upvotes: 1
Reputation: 254501
strcpy
works with zero-terminated strings, not arbitrary lumps of memory.
If you have filled it with a terminated string, then strcpy
should work; if it doesn't, please give more information about how it "doesn't work".
If you don't want a terminated string, then use memcpy
:
memcpy(temp, buf, BUFSIZ);
Note that there's no need to multiply by sizeof(char)
, since that is 1 by definition.
If you're actually writing C++, then you probably want to use std::vector
or std::string
rather than messing around with raw memory.
Upvotes: 1
Reputation: 98489
You don't have a terminating NUL
character in your buf
. You should make sure to do buf[last_actually_used_char+1] = '\0';
. This means buf
will have to be one character larger than the data you wish to store.
This is necessary because strcpy
finds the length of the information to be copied by searching for a terminating NUL
.
I strongly encourage the use of the safer strncpy
, or if you just want to copy data (no '\0'
necessary) the faster and safer memcpy
.
Upvotes: 0
Reputation: 55573
What you probably want is strncpy()
, which copies up to a certain number of bytes from a string.
strncpy(temp, buf, BUFSIZ - 1);
temp[BUFSIZ - 1] = '\0'; // null terminate the string
If that too fails, possibly just use memcpy()
memcpy(tmp, buf, BUFSIZ - 1);
temp[BUFSIZ - 1] = '\0'; // null terminate the string
Upvotes: 1