What's the difference between strcpy and stpcpy?

后端 未结 6 936
不知归路
不知归路 2021-01-01 12:57

While reading the man page for strcpy, I discovered the function stpcpy also exists. However, the only difference I could notice in the man page is

6条回答
  •  有刺的猬
    2021-01-01 13:47

    The stpcpy() function copies the string pointed to by src (including the terminating '\0' character) to the array pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy.

    This satisfies the requirements for restrict, even if it's not in the function signature. If C99 is present, it could be added.

    stpcpy() returns a pointer to the end of the string dest (that is, the address of the terminating null byte) rather than the beginning.

    This enables you to concatenate many strings more easily, and more efficiently, as src can be "tacked" onto the pointer returned from the last stpcpy(). The naive implementation of the alternative, strcat has to locate the end of the dest string before it can commence copying.

    Note the following:

    This function is not part of the C or POSIX.1 standards, and is not customary on Unix systems, but is not a GNU invention either. Perhaps it comes from MS-DOS. Nowadays, it is also present on the BSDs.

提交回复
热议问题