strcpy - copy a string to a location YOU created (you create the location, make sure that the source string will have enough room there and afterwards use strcpy to copy)
strdup - copy a string to a location that will be created by the function. The function will allocate space, make sure that your string will fit there and copy the string. Will return a pointer to the created area.
In short, you don't. strncpy is deemed unsafe as it has potential to cause buffer overruns. To copy strings safely in C++, use std::string instead. For examples and syntax, see related links, below.
They do different things, so they are uncomparable.PS: strcpy can be implemented with strlen+memcpy:char *strcpy (char *dest, const char *src){size_t len;len= strlen (src);memcpy (dest, src, len);return dest;}
char* strcpy(const char* src, char* dst) { char* tmp = dst; while ((*dst++ = *src++) != '\0'); return tmp; }
The standard library is your friend here - look for 'strlcpy' Without error checking, it should look something like this... // START CODE // we need an extra char // for the null character at the end - so we need j + 1 chars char * s2 = malloc( sizeof(char) * (j + 1 ) ); strlcpy( s2, s1 + j, j + 1 ); // END CODE Hope this helps. EDIT::::::: Oops... strlcpy is not actually part of the C standard library... :( It ought to be kicking around on most Linux distros... If your environment does not provide strlcpy, use its cousin, strncpy to do this with strncpy... (strncpy has poorer error handling than strlcpy imo, but they operate very similarly) // START CODE char * s2 = malloc( sizeof( char ) * ( j + 1 ) ); strncpy( s2, s1 + j, j + 1); s2[j] = '\0'; // strncpy does not guarantee a null-terminated result :( // END CODE Hope this helps - and isn't broken ;)
As usual, you should check official documentation before you ask a question like this. string.h // Copies num characters from source into destination. char* strncpy (char* destination, const char* source, size_t num); // Copies characters from source into destination. char* strcpy (char* destination, const char* source);
strcpy is meant to copy only null-terminated strings. It is probably implemented to copy every byte until it encounters a #0. strncpy() copies n bytes and it adds null termination at the end of the target string.
use strcat, strncpy, stpcpy, sprintf, strlen+memcpy, etc
In short, you don't. strncpy is deemed unsafe as it has potential to cause buffer overruns. To copy strings safely in C++, use std::string instead. For examples and syntax, see related links, below.
memcpy is general purpose copy. and strcpy is specific for string copying. strcpy will copy the source string to destination string and terminate it with '\0' character but memcpy takes extra argument which specifies the number of bytes to copy.memcpy will not handle copying of overlapping memory. use memove instead.
Using strcpy and strcat. Or sprintf. Or strlen+memcpy. There are more than solutions.
They do different things, so they are uncomparable.PS: strcpy can be implemented with strlen+memcpy:char *strcpy (char *dest, const char *src){size_t len;len= strlen (src);memcpy (dest, src, len);return dest;}
strcpy
char* strcpy(const char* src, char* dst) { char* tmp = dst; while ((*dst++ = *src++) != '\0'); return tmp; }
difference between as on and as at
The standard library is your friend here - look for 'strlcpy' Without error checking, it should look something like this... // START CODE // we need an extra char // for the null character at the end - so we need j + 1 chars char * s2 = malloc( sizeof(char) * (j + 1 ) ); strlcpy( s2, s1 + j, j + 1 ); // END CODE Hope this helps. EDIT::::::: Oops... strlcpy is not actually part of the C standard library... :( It ought to be kicking around on most Linux distros... If your environment does not provide strlcpy, use its cousin, strncpy to do this with strncpy... (strncpy has poorer error handling than strlcpy imo, but they operate very similarly) // START CODE char * s2 = malloc( sizeof( char ) * ( j + 1 ) ); strncpy( s2, s1 + j, j + 1); s2[j] = '\0'; // strncpy does not guarantee a null-terminated result :( // END CODE Hope this helps - and isn't broken ;)
What is the difference between Florida and California What is the difference between Florida and California
/* this is the same functionality as strcpy() */ /* note that this, like strcpy, is not buffer overrun safe */ char *StringCopy (char *destination, const char *source) { const char *temp = destination; while ((*destination++ = *source++) != '\0'); return temp; }