Replies
Welcome, guest
Join CrazyEngineers to reply, ask questions, and participate in conversations.
CrazyEngineers powered by Jatra Community Platform
-
@friendster7-oVJr9h • Mar 2, 2008
char <a href="https://en.wikipedia.org/wiki/Strcpy" target="_blank" rel="nofollow noopener noreferrer">C string handling - Wikipedia</a>(char *, const char *); copies a string from one location to another -
@friendster7-oVJr9h • Mar 2, 2008
let us say
char *s1; points to an area of memory that is to receive the copied characters. const char *s2; points to the string from which characters will be copied. This must end with the usual '\0'. -
@pradeep-agrawal-rhdX5z • Mar 2, 2008
The prototype is: void strcpy(char *buffer, char const *string);
The function copies the string pointed by variable 'string' to variable 'buffer', i.e., it copies each character starting from the memory location pointed by variable 'string' to to the memory location starting from the one pointed by variable 'buffer'. The function copies the characters till it encounter character '/0' (character having ASCII value 0, represents end of string).
The use of const in 'char const *string' means that variable 'string' is pointer to constant characters, so you can't modify the string represented by variable 'string'.
-Pradeep -
@pradeep-agrawal-rhdX5z • Mar 2, 2008
But its better to use errno_t strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource); instead of strcpy.
Consider case that your destination buffer is of size 128 and source string has length greater then 128. In such case the code will cause crash.
In strcpy_s, 'size_t numberOfElements' specifies the size of destination string buffer and it returns 0 if copy is successful otherwise returns the error code.
-Pradeep