连续调用该函数会返回相同的结果

时间:2011-04-07 20:20:19

标签: c parsing

我正在尝试从字符串中提取网址。所以我写了这样的东西:

#include "string.h"
#include "stdio.h"
#define BAD(x) (!(x) || (*(x) == '\0'))

static char *extract_link(char *string)
{
        static char url[512];
        int length;
        char *st;
        char *rl;
        char *rl2;
        url[0] = '\0';

        rl = strstr(string, "http://");
        if (!BAD(rl))
        {
                if (strstr(rl, " "))
                {
                        st = strstr(rl, " ");
                        length = strlen(rl) - strlen(st);
                        strncpy(url, rl, length);
                        url[length] = '\0';
                        return url;
                }
                return rl;
        }
        rl2 = strstr(string, "www.");
        if (!BAD(rl2))
        {
                if (strchr(rl2, ' '))
                {
                        st = strstr(rl2, " ");
                        length = strlen(rl2) - strlen(st);
                        strncpy(url, rl2, length);
                        url[length] = '\0';
                        return url;
                }
                return rl2;
        }
        return NULL;
}
int main()
{
        char *string1 = "We have a http://www.youtube.com/watch?v=zyP48_0C1DM&feature=featured here";
        char *string2 = "And We have a www.scribd.com/doc/52344388/JOINT-STATEMENT-ON-U-S-FUNDING-FOR-GLOBAL-CHILD-SURVIVAL-AND-MATERNAL-HEALTH or whatsoever";
        char *x = extract_link(string1), *y = extract_link(string2);
        if (!BAD(x))
                printf("%s\n", x);
        if (!BAD(y))
                printf("%s\n", y);
        return 0;
}

但我一遍又一遍地得到同样的结果。 输出:

  

www.scribd.com/doc/52344388/JOINT-STATEMENT-ON-U-S-FUNDING-FOR-GLOBAL-CHILD-SURVIVAL-AND-MATERNAL-HEALTH   www.scribd.com/doc/52344388/JOINT-STATEMENT-ON-U-S-FUNDING-FOR-GLOBAL-CHILD-SURVIVAL-AND-MATERNAL-HEALTH

应该是这样的:

  

http://www.youtube.com/watch?v=5fuUTMJ8WRA&feature=feedrec_grec_index   www.scribd.com/doc/52344388/JOINT-STATEMENT-ON-U-S-FUNDING-FOR-GLOBAL-CHILD-SURVIVAL-AND-MATERNAL-HEALTH

所以... 我做错了什么?您如何看待上面的代码?我需要改变或改写吗?

1 个答案:

答案 0 :(得分:3)

这样做是因为extract_link()会覆盖静态缓冲区并返回指向它的指针。相反,返回指向原始字符串的指针,或创建一个动态分配的缓冲区以指向。

每种方法都有权衡。对于您使用的那个,您可以不管它并将main更改为:

char *string1 = "We have a http://www.youtube.com/watch?v=zyP48_0C1DM&feature=featured here";
char *string2 = "And We have a www.scribd.com/doc/52344388/JOINT-STATEMENT-ON-U-S-FUNDING-FOR-GLOBAL-CHILD-SURVIVAL-AND-MATERNAL-HEALTH or whatsoever";
char *x = extract_link(string1);
if (!BAD(x))
      printf("%s\n", x);
x = extract_link(string2);
if (!BAD(x))
       printf("%s\n", x);
return 0;