answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

6y ago

memcpy will copy NULL bytes also, but strcpy will stop copying if it encounters NULL Byte, since it consider NULL as a terminating character.

cheers,

som shekhar

And another thing is if we want to copy two integer arrays or some other memory which are not strings we can use memcpy. strcpy/strncpy can be used for integer copies after typecasting but the behavior is unpridictable.

Strcpy usually accepts the char* as an argument but the memcpy accepts the void * as an argument.

-------Ravi

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

strcpy copies the entire string including the byte with the zero value that is the terminal character of a string.

strncpy copies at most n character or up to and including byte with the zero value is the string length is less than n.

If only n characters are copied the string will not always be zero terminated, be warned overlooking this fact is a quick way to a crash.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

The best way to understand how any function works is to look at the implementation.

strcmp()

Here is the implementation for the strcmp function in C:

int strcmp (const char* s1, const char* s2) {

while (*s1 && (*s1==*s2)) s1++,s2++;

return *(const unsigned char*)s1-*(const unsigned char*)s2;

}

Despite its small size, there's quite a lot going on here. From the function prototype we can see that the function accepts two pointers to const char and returns an int.

The function body has a while loop which increments the pointers (s1++, s2++) so long as the following boolean expression holds true:

*s1 && (*s1==*s2)

The && is a logical AND and this has two operands, each of which is also a boolean expression.

The expression *s1 dereferences the value being pointed at by s1. That is; the expression evaluates the character code. If the code is zero then the expression evaluates false, otherwise it evaluates true.

The expression (*s1==*s2) dereferences the values pointed at by s1 and s2 and compares them for equality. If they are not equal, the expression evaluates false, otherwise it evaluates true.

For the whole expression to evaluate true, both operands must evaluate true. Note that if the first evaluates false then there is no need to evaluate the second; the entire expression is false regardless of whether the second is true or false.

From this we can see that s1 and s2 will iteratively advance through the characters in their respective strings until either s1 points to null or the two characters differ in some way.

The final line looks more complex than it really is; it simply returns the difference between the two character codes currently being pointed at. The explicit casts to const unsigned char are necessary because the C-standard does not define a "plain" char to be signed or unsigned (it is implementation-defined).

If the difference is zero then it means both s1 and s2 point to null terminators. It therefore follows that any and all of preceding characters matched exactly, thus the strings are the same. If the difference is non-zero, then the strings are not the same. If the difference is negative, the first string is lexicographically "less than" the second, otherwise it is "greater than".

The implementation also tells us that the function has undefined behaviour when either string is not null-terminated.

strcpy()

The strcpy function has the following implementation:

void strcpy (char* dst, const char* src) {

while ((*dst=*src)!='\0') dst++, src++;

}

Here we can see that the character pointed to by src is assigned to the memory pointed to by dst and the pointers advanced by one character. This repeats until the last copied character was a null-terminator ('\0'). Note that (*dst=*src) evaluates to the value of *dst after assignment and that this value can be used as the left-hand operand of the binary not-equal operator (!=).

From the implementation we can also see that the function has undefined behaviour when the source string is not null-terminated or the destination array is not large enough to cater for the source string.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between strcpy and strncpy?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why you are using strcpy in c plus plus?

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.


What is better strcpy or memcpy?

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;}


Pointer version of string function strcpy?

char* strcpy(const char* src, char* dst) { char* tmp = dst; while ((*dst++ = *src++) != '\0'); return tmp; }


Write a c function to extract a substring s2 of j characters starting from jth position of a given string s1?

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 ;)


Program to copy one string to another?

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);

Related questions

What is the difference between strcpy and strncpy in c language?

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.


Write a c program to copy the string without using strcpy?

use strcat, strncpy, stpcpy, sprintf, strlen+memcpy, etc


Why you are using strcpy in c plus plus?

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.


What are the differences between memcpy vs strcpy?

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.


To copy two strings using strcpy?

Using strcpy and strcat. Or sprintf. Or strlen+memcpy. There are more than solutions.


What is better strcpy or memcpy?

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;}


C program plus copy of string to another?

strcpy


Pointer version of string function strcpy?

char* strcpy(const char* src, char* dst) { char* tmp = dst; while ((*dst++ = *src++) != '\0'); return tmp; }


Write a c function to extract a substring s2 of j characters starting from jth position of a given string s1?

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 difference between as on and as at?

difference between as on and as at


C programming copying of two strings?

/* 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; }


Program to copy one string to another?

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);