有谁记得strcpy函数的原型
有谁记得strcpy函数的原型,
就是STRCPY是怎么样编写的。
问题点数:100、回复次数:15Top
1 楼prototype(原型)回复于 2002-03-28 16:25:45 得分 10
char* strcpy(char* s, const char* ct);
Copies ct to s including terminating NUL and returns s.
Top
2 楼neccui(PPC)回复于 2002-03-28 16:26:26 得分 0
char *strcpy(char *dest, const char *src);
怎么编写?随便了,很多种写法。随手写一个。
char *strcpy(char *dest, const char *src)
{
if(dest == NULL) return NULL;
char * tmp=dest;
do
{
*(tmp++)=*src;
}while(*(src++));
return dest;
}Top
3 楼prototype(原型)回复于 2002-03-28 16:27:52 得分 0
char* strcpy(char* s, const char* ct)
{
while (*s++ = *ct++);
}Top
4 楼prototype(原型)回复于 2002-03-28 16:28:26 得分 20
char* strcpy(char* s, const char* ct)
{
while (*s++ = *ct++);
return s;
}Top
5 楼lifenqidelphi(血)回复于 2002-03-28 16:28:43 得分 0
我是说char* strcpy(char* s, const char* ct)是怎么用语句实现的Top
6 楼cococut(小鱼的天空)回复于 2002-03-28 16:29:05 得分 10
#include <string.h>
#include <stdio.h>
void main( void )
{
char string[80];
strcpy( string, "Hello world from " );
strcat( string, "strcpy " );
strcat( string, "and " );
strcat( string, "strcat!" );
printf( "String = %s\n", string );
}
Output
String = Hello world from strcpy and strcat!
Top
7 楼neccui(PPC)回复于 2002-03-28 16:31:01 得分 30
to prototype(原型) 你的代码是错误的。
应该这样才对。
char* strcpy(char* s, const char* ct)
{
char *tmp = s;
while (*s++ = *ct++);
return tmp;
}
Top
8 楼lifenqidelphi(血)回复于 2002-03-28 16:32:51 得分 0
我的意思是microsoft写的原型。Top
9 楼prototype(原型)回复于 2002-03-28 16:35:21 得分 0
neccui(PPC) is right.Top
10 楼prototype(原型)回复于 2002-03-28 16:37:07 得分 0
neccui(PPC) is right.Top
11 楼neccui(PPC)回复于 2002-03-28 16:40:19 得分 0
to lifenqidelphi(血) , 不要用错误的术语。
所谓原型,就是指。
char* strcpy(char* s, const char* ct);
这一句话。
你想要的是"实现",不是原型。Top
12 楼lifenqidelphi(血)回复于 2002-03-28 16:45:59 得分 0
哈哈哈。。。对对,是应说怎么实现。不过大家能理解是这个意思
就行了嘛。我是说的microsoft是怎么实现的。而不是我们自己
用的相同的方法。Top
13 楼gigix(透明)回复于 2002-03-28 16:58:33 得分 30
Visual Studio .NET里面有源代码:
/***
*char *strcpy(dst, src) - copy one string over another
*
*Purpose:
* Copies the string src into the spot specified by
* dest; assumes enough room.
*
*Entry:
* char * dst - string over which "src" is to be copied
* const char * src - string to be copied over "dst"
*
*Exit:
* The address of "dst"
*
*Exceptions:
****************************************************************/
char * __cdecl strcpy(char * dst, const char * src)
{
char * cp = dst;
while( *cp++ = *src++ ); /* Copy src over dst */
return( dst );
}
我有点想不通:为什么不用memcpy来实现呢?谁能告诉我?Top
14 楼lifenqidelphi(血)回复于 2002-03-28 17:11:29 得分 0
memcpy的原型 为
void *memcpy(void *to,const *from,size_t count);
它是复制conunt个字符。Top
15 楼prototype(原型)回复于 2002-03-28 17:13:57 得分 0
because you don't know the length of the string 'scr'.
furthermore, 'memcpy' is likely implemented in a similar way.Top
16 楼neccui(PPC)回复于 2002-03-28 17:15:16 得分 0
to gigix:
基于性能考虑。Top
17 楼lifenqidelphi(血)回复于 2002-06-13 12:48:39 得分 0
kkkkTop




