pallavi agarwal
pallavi agarwal
Branch Unspecified
02 Mar 2008

C problem: What does void strcpy(char *buffer, char const *string) mean?

what does this statement mean??
void strcpy(char *buffer, char const *string)
friendster7

friendster7

Branch Unspecified
02 Mar 2008
char strcpy(char *, const char *); copies a string from one location to another
friendster7

friendster7

Branch Unspecified
02 Mar 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

pradeep_agrawal

Branch Unspecified
03 Mar 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

pradeep_agrawal

Branch Unspecified
03 Mar 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

Share this content on your social channels -

Only logged in users can reply.