编译出错,大家帮忙看看
下面的函数目地是搜索第N个子串出现的位置,并截掉后面或前面部份(用N是正负表示)在做成一个程序时测试成功,但在做成函数形式时,编译出错,大家帮我看看,谢谢了
[root@localhost livecore.cc]# cat trimrstr.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
char link[500];
char *string="<TR><td width=25><img src=img/space.gif border=0 width=1height=2></td><TD class=menuCountry><A class=menuLink href=index.php?s=21&lang=en&id=&g=s&g_m=s&g_i=&l=&c=16&live=true&result=><B>USA</B></A></TD><td width=15><img src=img/space.gif border=0 width=1 height=2></td></TR>";
int repeat=3;
char *sub="img";
strcpy(link,trimstr(string,repeat,sub));
printf("%s",link);
}
char *
trimstr(char *string,int repeat,char *sub)
{
char *current;
char *last;
char dest[strlen(string)];
int i;
printf("%s\n\n",string);
current=string;
for (i=1;i <= abs(repeat);i++)
{
last = current;
current=strstr(last+1,sub);
}
if (repeat > 0)
{
memcpy(dest,current+strlen(sub),strlen(current+strlen(sub)));
}
else
{
memcpy(dest,string,strlen(string)-strlen(current));
}
printf("%s\n",dest);
return dest;
}
编译出错信息如下
[root@localhost livecore.cc]# gcc trimrstr.c
trimrstr.c: In function `main':
trimrstr.c:11: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast
trimrstr.c: At top level:
trimrstr.c:18: warning: type mismatch with previous implicit declaration
trimrstr.c:11: warning: previous implicit declaration of `trimstr'
trimrstr.c:18: warning: `trimstr' was previously implicitly declared to return `int'
trimrstr.c: In function `trimstr':
trimrstr.c:41: warning: function returns address of local variable