我的大学建议我从这份文档中学习C
成为一名Java
程序员:“<a href="http://faculty.ksu.edu.sa/jebari_chaker/papers/C_for_Java_Programmers.pdf" rel="nofollow noreferrer">C for Java程序员”,J. Maassen 着。
在第 46 页(PDF-page 47),Maassen 尝试实现他自己版本的C
'sstrcpy
函数,称为my_strcpy
char *my_strcpy(char *destination, char *source)
{
char*p = destination;
while (*source != '\0')
{
*p++ = *source++;
}
*p = '\0';
return destination;
}
我试图用这个函数编写一个程序。
查看第 45 页(PDF-第 46 页)。在这里,Maassen 介绍了他的第一个版本的 astrcpy
方法。他包括stdio.h
并复制strA
到strB
.
那么,下面的程序应该可以工作,不是吗?
#include <stdio.h>
char strA[80] = "A string to be used for demonstration purposes";
char strB[80];
int main(void)
{
my_strcpy(strB, strA);
puts(strB);
}
char *my_strcpy(char *destination, char *source)
{
char*p = destination;
while (*source != '\0')
{
*p++ = *source++;
}
*p = '\0';
return destination;
}
嗯,实际上并没有。
因为每次我编译这个程序时,我都会收到以下错误:
PROGRAM.c:12:7: error: conflicting types for ‘my_strcpy’
char *my_strcpy(char *destination, char *source)
^
PROGRAM.c:8:5: note: previous implicit declaration of ‘my_strcpy’ was here
mystrcpy(strB, strA);
^
为什么这个程序不起作用?我的意思是,它应该工作,不是吗?
我在这里strcpy
看到了一个类似的函数实现。而且该实施也不起作用!我遇到了同样的错误!
怎么了?