user-0 提问: strcpy()为什么返回指针呢

回复:
user: user-1
content: 引用 9 楼 turingo 的回复:你说的没错，链式表达是很重要的一个原因，另外也可以直接判断成功和失败。  [quote=引用 8 楼 jack_leiwin 的回复:] [quote=引用 4 楼 turingo 的回复:]  NAME        strcpy, strncpy - copy a string  SYNOPSIS        #include <string.h>         char *strcpy(char *dest, const char *src);         char *strncpy(char *dest, const char *src, size_t n);  DESCRIPTION        The  strcpy()  function  copies the string pointed to by src, including        the terminating null byte ('\0'), to the buffer  pointed  to  by  dest.        The  strings  may  not overlap, and the destination string dest must be        large enough to receive the copy.         The strncpy() function is similar, except that at most n bytes  of  src        are  copied.  Warning: If there is no null byte among the first n bytes        of src, the string placed in dest will not be null-terminated.         If the length of src is less than n, strncpy() pads  the  remainder  of        dest with null bytes.         A simple implementation of strncpy() might be:             char *            strncpy(char *dest, const char *src, size_t n)            {                size_t i;                 for (i = 0; i < n && src[i] != '\0'; i++)                    dest[i] = src[i];                for ( ; i < n; i++)                    dest[i] = '\0';                 return dest;            }  RETURN VALUE        The  strcpy()  and strncpy() functions return a pointer to the destina‐        tion string dest.  CONFORMING TO        SVr4, 4.3BSD, C89, C99.  NOTES        Some programmers consider strncpy() to be inefficient and error  prone.        If  the  programmer knows (i.e., includes code to test!)  that the size        of dest is greater than the length of src, then strcpy() can be used.         If there is no terminating null byte in the first n characters of  src,        strncpy()  produces  an unterminated string in dest.  Programmers often        prevent this mistake by forcing termination as follows:             strncpy(buf, str, n);            if (n > 0)                buf[n - 1]= '\0';  BUGS        If the destination string of a strcpy() is not large enough, then  any‐        thing  might  happen.   Overflowing  fixed-length  string  buffers is a        favorite cracker technique for taking complete control of the  machine.        Any  time  a  program  reads  or copies data into a buffer, the program        first needs to check that there's enough space.  This may  be  unneces‐        sary  if you can show that overflow is impossible, but be careful: pro‐        grams can get changed over time, in ways that may make  the  impossible        possible.   [quote=引用 楼主 hou1056105902 的回复:] strcpy函数原型为char *strcpy(char *strDest, const char *strSrc); 为什么返回指针呢？还有我们平常用时像： strcpy(s1,s2); 根本也没返回值啊？？求解[/quote]   前辈，您好，strcpy这个返回值是面试过程中面试官问的一个问题，我自己在用这个函数的时候 也不会用这个函数的返回值，在网上查了一下说是为了链式表达，比如：int length = strlen( strcpy( strDest, “hello world”) )， 我想了想，这个理由有点强迁，前辈您觉得呢？[/quote][/quote]多谢！

user: user-2
content: 你说的没错，链式表达是很重要的一个原因，另外也可以直接判断成功和失败。  引用 8 楼 jack_leiwin 的回复:[quote=引用 4 楼 turingo 的回复:]  NAME        strcpy, strncpy - copy a string  SYNOPSIS        #include <string.h>         char *strcpy(char *dest, const char *src);         char *strncpy(char *dest, const char *src, size_t n);  DESCRIPTION        The  strcpy()  function  copies the string pointed to by src, including        the terminating null byte ('\0'), to the buffer  pointed  to  by  dest.        The  strings  may  not overlap, and the destination string dest must be        large enough to receive the copy.         The strncpy() function is similar, except that at most n bytes  of  src        are  copied.  Warning: If there is no null byte among the first n bytes        of src, the string placed in dest will not be null-terminated.         If the length of src is less than n, strncpy() pads  the  remainder  of        dest with null bytes.         A simple implementation of strncpy() might be:             char *            strncpy(char *dest, const char *src, size_t n)            {                size_t i;                 for (i = 0; i < n && src[i] != '\0'; i++)                    dest[i] = src[i];                for ( ; i < n; i++)                    dest[i] = '\0';                 return dest;            }  RETURN VALUE        The  strcpy()  and strncpy() functions return a pointer to the destina‐        tion string dest.  CONFORMING TO        SVr4, 4.3BSD, C89, C99.  NOTES        Some programmers consider strncpy() to be inefficient and error  prone.        If  the  programmer knows (i.e., includes code to test!)  that the size        of dest is greater than the length of src, then strcpy() can be used.         If there is no terminating null byte in the first n characters of  src,        strncpy()  produces  an unterminated string in dest.  Programmers often        prevent this mistake by forcing termination as follows:             strncpy(buf, str, n);            if (n > 0)                buf[n - 1]= '\0';  BUGS        If the destination string of a strcpy() is not large enough, then  any‐        thing  might  happen.   Overflowing  fixed-length  string  buffers is a        favorite cracker technique for taking complete control of the  machine.        Any  time  a  program  reads  or copies data into a buffer, the program        first needs to check that there's enough space.  This may  be  unneces‐        sary  if you can show that overflow is impossible, but be careful: pro‐        grams can get changed over time, in ways that may make  the  impossible        possible.   [quote=引用 楼主 hou1056105902 的回复:] strcpy函数原型为char *strcpy(char *strDest, const char *strSrc); 为什么返回指针呢？还有我们平常用时像： strcpy(s1,s2); 根本也没返回值啊？？求解[/quote]   前辈，您好，strcpy这个返回值是面试过程中面试官问的一个问题，我自己在用这个函数的时候 也不会用这个函数的返回值，在网上查了一下说是为了链式表达，比如：int length = strlen( strcpy( strDest, “hello world”) )， 我想了想，这个理由有点强迁，前辈您觉得呢？[/quote]

user: user-1
content: 引用 4 楼 turingo 的回复: NAME        strcpy, strncpy - copy a string  SYNOPSIS        #include <string.h>         char *strcpy(char *dest, const char *src);         char *strncpy(char *dest, const char *src, size_t n);  DESCRIPTION        The  strcpy()  function  copies the string pointed to by src, including        the terminating null byte ('\0'), to the buffer  pointed  to  by  dest.        The  strings  may  not overlap, and the destination string dest must be        large enough to receive the copy.         The strncpy() function is similar, except that at most n bytes  of  src        are  copied.  Warning: If there is no null byte among the first n bytes        of src, the string placed in dest will not be null-terminated.         If the length of src is less than n, strncpy() pads  the  remainder  of        dest with null bytes.         A simple implementation of strncpy() might be:             char *            strncpy(char *dest, const char *src, size_t n)            {                size_t i;                 for (i = 0; i < n && src[i] != '\0'; i++)                    dest[i] = src[i];                for ( ; i < n; i++)                    dest[i] = '\0';                 return dest;            }  RETURN VALUE        The  strcpy()  and strncpy() functions return a pointer to the destina‐        tion string dest.  CONFORMING TO        SVr4, 4.3BSD, C89, C99.  NOTES        Some programmers consider strncpy() to be inefficient and error  prone.        If  the  programmer knows (i.e., includes code to test!)  that the size        of dest is greater than the length of src, then strcpy() can be used.         If there is no terminating null byte in the first n characters of  src,        strncpy()  produces  an unterminated string in dest.  Programmers often        prevent this mistake by forcing termination as follows:             strncpy(buf, str, n);            if (n > 0)                buf[n - 1]= '\0';  BUGS        If the destination string of a strcpy() is not large enough, then  any‐        thing  might  happen.   Overflowing  fixed-length  string  buffers is a        favorite cracker technique for taking complete control of the  machine.        Any  time  a  program  reads  or copies data into a buffer, the program        first needs to check that there's enough space.  This may  be  unneces‐        sary  if you can show that overflow is impossible, but be careful: pro‐        grams can get changed over time, in ways that may make  the  impossible        possible.   [quote=引用 楼主 hou1056105902 的回复:] strcpy函数原型为char *strcpy(char *strDest, const char *strSrc); 为什么返回指针呢？还有我们平常用时像： strcpy(s1,s2); 根本也没返回值啊？？求解[/quote]   前辈，您好，strcpy这个返回值是面试过程中面试官问的一个问题，我自己在用这个函数的时候 也不会用这个函数的返回值，在网上查了一下说是为了链式表达，比如：int length = strlen( strcpy( strDest, “hello world”) )， 我想了想，这个理由有点强迁，前辈您觉得呢？

user: user-2
content: 说实在的，要想学好编程，英语至少要看得懂的。  引用 6 楼 hou1056105902 的回复:[quote=引用 4 楼 turingo 的回复:]  NAME        strcpy, strncpy - copy a string  SYNOPSIS        #include <string.h>         char *strcpy(char *dest, const char *src);         char *strncpy(char *dest, const char *src, size_t n);  DESCRIPTION        The  strcpy()  function  copies the string pointed to by src, including        the terminating null byte ('\0'), to the buffer  pointed  to  by  dest.        The  strings  may  not overlap, and the destination string dest must be        large enough to receive the copy.         The strncpy() function is similar, except that at most n bytes  of  src        are  copied.  Warning: If there is no null byte among the first n bytes        of src, the string placed in dest will not be null-terminated.         If the length of src is less than n, strncpy() pads  the  remainder  of        dest with null bytes.         A simple implementation of strncpy() might be:             char *            strncpy(char *dest, const char *src, size_t n)            {                size_t i;                 for (i = 0; i < n && src[i] != '\0'; i++)                    dest[i] = src[i];                for ( ; i < n; i++)                    dest[i] = '\0';                 return dest;            }  RETURN VALUE        The  strcpy()  and strncpy() functions return a pointer to the destina‐        tion string dest.  CONFORMING TO        SVr4, 4.3BSD, C89, C99.  NOTES        Some programmers consider strncpy() to be inefficient and error  prone.        If  the  programmer knows (i.e., includes code to test!)  that the size        of dest is greater than the length of src, then strcpy() can be used.         If there is no terminating null byte in the first n characters of  src,        strncpy()  produces  an unterminated string in dest.  Programmers often        prevent this mistake by forcing termination as follows:             strncpy(buf, str, n);            if (n > 0)                buf[n - 1]= '\0';  BUGS        If the destination string of a strcpy() is not large enough, then  any‐        thing  might  happen.   Overflowing  fixed-length  string  buffers is a        favorite cracker technique for taking complete control of the  machine.        Any  time  a  program  reads  or copies data into a buffer, the program        first needs to check that there's enough space.  This may  be  unneces‐        sary  if you can show that overflow is impossible, but be careful: pro‐        grams can get changed over time, in ways that may make  the  impossible        possible.   [quote=引用 楼主 hou1056105902 的回复:] strcpy函数原型为char *strcpy(char *strDest, const char *strSrc); 为什么返回指针呢？还有我们平常用时像： strcpy(s1,s2); 根本也没返回值啊？？求解[/quote]   4楼的看不懂啊[/quote]

user: user-0
content: 引用 4 楼 turingo 的回复: NAME        strcpy, strncpy - copy a string  SYNOPSIS        #include <string.h>         char *strcpy(char *dest, const char *src);         char *strncpy(char *dest, const char *src, size_t n);  DESCRIPTION        The  strcpy()  function  copies the string pointed to by src, including        the terminating null byte ('\0'), to the buffer  pointed  to  by  dest.        The  strings  may  not overlap, and the destination string dest must be        large enough to receive the copy.         The strncpy() function is similar, except that at most n bytes  of  src        are  copied.  Warning: If there is no null byte among the first n bytes        of src, the string placed in dest will not be null-terminated.         If the length of src is less than n, strncpy() pads  the  remainder  of        dest with null bytes.         A simple implementation of strncpy() might be:             char *            strncpy(char *dest, const char *src, size_t n)            {                size_t i;                 for (i = 0; i < n && src[i] != '\0'; i++)                    dest[i] = src[i];                for ( ; i < n; i++)                    dest[i] = '\0';                 return dest;            }  RETURN VALUE        The  strcpy()  and strncpy() functions return a pointer to the destina‐        tion string dest.  CONFORMING TO        SVr4, 4.3BSD, C89, C99.  NOTES        Some programmers consider strncpy() to be inefficient and error  prone.        If  the  programmer knows (i.e., includes code to test!)  that the size        of dest is greater than the length of src, then strcpy() can be used.         If there is no terminating null byte in the first n characters of  src,        strncpy()  produces  an unterminated string in dest.  Programmers often        prevent this mistake by forcing termination as follows:             strncpy(buf, str, n);            if (n > 0)                buf[n - 1]= '\0';  BUGS        If the destination string of a strcpy() is not large enough, then  any‐        thing  might  happen.   Overflowing  fixed-length  string  buffers is a        favorite cracker technique for taking complete control of the  machine.        Any  time  a  program  reads  or copies data into a buffer, the program        first needs to check that there's enough space.  This may  be  unneces‐        sary  if you can show that overflow is impossible, but be careful: pro‐        grams can get changed over time, in ways that may make  the  impossible        possible.   [quote=引用 楼主 hou1056105902 的回复:] strcpy函数原型为char *strcpy(char *strDest, const char *strSrc); 为什么返回指针呢？还有我们平常用时像： strcpy(s1,s2); 根本也没返回值啊？？求解[/quote]   4楼的看不懂啊

user: user-3
content: 谁说没有返回值。。。。。。。。。 只是你没有用变量去接收而已，strcpy返回拷贝后的目标字符串。

user: user-2
content: NAME        strcpy, strncpy - copy a string  SYNOPSIS        #include <string.h>         char *strcpy(char *dest, const char *src);         char *strncpy(char *dest, const char *src, size_t n);  DESCRIPTION        The  strcpy()  function  copies the string pointed to by src, including        the terminating null byte ('\0'), to the buffer  pointed  to  by  dest.        The  strings  may  not overlap, and the destination string dest must be        large enough to receive the copy.         The strncpy() function is similar, except that at most n bytes  of  src        are  copied.  Warning: If there is no null byte among the first n bytes        of src, the string placed in dest will not be null-terminated.         If the length of src is less than n, strncpy() pads  the  remainder  of        dest with null bytes.         A simple implementation of strncpy() might be:             char *            strncpy(char *dest, const char *src, size_t n)            {                size_t i;                 for (i = 0; i < n && src[i] != '\0'; i++)                    dest[i] = src[i];                for ( ; i < n; i++)                    dest[i] = '\0';                 return dest;            }  RETURN VALUE        The  strcpy()  and strncpy() functions return a pointer to the destina‐        tion string dest.  CONFORMING TO        SVr4, 4.3BSD, C89, C99.  NOTES        Some programmers consider strncpy() to be inefficient and error  prone.        If  the  programmer knows (i.e., includes code to test!)  that the size        of dest is greater than the length of src, then strcpy() can be used.         If there is no terminating null byte in the first n characters of  src,        strncpy()  produces  an unterminated string in dest.  Programmers often        prevent this mistake by forcing termination as follows:             strncpy(buf, str, n);            if (n > 0)                buf[n - 1]= '\0';  BUGS        If the destination string of a strcpy() is not large enough, then  any‐        thing  might  happen.   Overflowing  fixed-length  string  buffers is a        favorite cracker technique for taking complete control of the  machine.        Any  time  a  program  reads  or copies data into a buffer, the program        first needs to check that there's enough space.  This may  be  unneces‐        sary  if you can show that overflow is impossible, but be careful: pro‐        grams can get changed over time, in ways that may make  the  impossible        possible.  引用 楼主 hou1056105902 的回复:strcpy函数原型为char *strcpy(char *strDest, const char *strSrc); 为什么返回指针呢？还有我们平常用时像： strcpy(s1,s2); 根本也没返回值啊？？求解

user: user-4
content: 还有一点，不是没有返回值，只是你没有用对应的变量去接受罢了， 一个函数有没有返回值，你都可以去调用它，但需不需要返回值就要看你自己怎么操作了， 对于strcpy的返回值就是你传进去给strDest的地址，你不去接受完全没有问题

user: user-4
content: 有返回值就可以一次复制任意个字符串，看你怎么用了， # include <stdio.h> # include <stdlib.h> # include <string.h>  int main() { 	char str[] = "hello";  	char str1[10], str2[10];  	strcpy(str1, strcpy(str2, str));  	return 0; }

user: user-5
content: 这样你就可以 strcpy(s3,strcpy(s1,s2)); 貌似我举得例子不太恰当，但就是这个意思