Chinaunix

标题: 帮忙纠正 调用strcpy的错误 [打印本页]

作者: heiniu532    时间: 2005-12-30 12:00
标题: 帮忙纠正 调用strcpy的错误
#include<stdio.h>
#include<string.h>

char * __cdecl strcpy(char * dst, const char * src)
{
        char * cp = dst;

        while( *cp++ = *src++ )
                ;               /* Copy src over dst */

        return( dst );
}
void main()
{
        char *p="abcdddddddddd",*pp="ddddd";
        strcpy(pp,p);
        printf("%10s",pp);
}


这个程序complie可以,但是build有2个错误。
--------------------Configuration: 1 - Win32 Debug--------------------
Linking...
LIBCD.lib(strcat.obj) : error LNK2005: _strcpy already defined in 1.obj
Debug/1.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

1.exe - 2 error(s), 0 warning(s)

请问是什么原因??

[ 本帖最后由 heiniu532 于 2005-12-30 12:39 编辑 ]
作者: deathbravo    时间: 2005-12-30 12:30
改个名字吧
作者: heiniu532    时间: 2005-12-30 12:39
ok
作者: aero    时间: 2005-12-30 12:58
char *p="abcdddddddddd",*pp="ddddd";
        strcpy(pp,p);

pp的空间为read-only。或者即使不是read-only,也不规范,要出警告的。
作者: heiniu532    时间: 2005-12-30 13:50
但是,即使把PP置为NULL,还是不行的。
作者: aero    时间: 2005-12-30 13:55
原帖由 heiniu532 于 2005-12-30 13:50 发表
但是,即使把PP置为NULL,还是不行的。


大哥,你还是没明白。NULL可以写么?C不仅仅是种语言。C在语言的范畴东西很少,C要了解的相关知识很多。

你可以从malloc函数开始看起。了解一下C中是怎么认识内存的。
作者: kernelxu    时间: 2005-12-30 14:06
  1. #include<stdio.h>
  2. #include<string.h>

  3. char * __cdecl my_strcpy(char * dst, const char * src) /*change another name*/
  4. {
  5.         char * cp = dst;

  6.         while( *cp++ = *src++ )
  7.                 ;               /* Copy src over dst */

  8.         return( dst );
  9. }
  10. /*void main() is not standard C*/
  11. int main(void)
  12. {
  13.         /*use arrays or pointers that have been allocated memory units*/
  14.         char p[]="abcdddddddddd";
  15.         char pp[100]="ddddd";
  16.         my_strcpy(pp,p); /*pp should reserve enough room for the string pointed by p*/
  17.         printf("%10s",pp);

  18.         return 0;
  19. }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2