strtok 和 strcpy 错误

发布于 2024-11-04 11:53:38 字数 912 浏览 1 评论 0原文

我使用 strtok 来分割字符串。

[更新] 我在下面的新版本中使用了您的评论和答案,但在 atm 上不起作用

int Crawl :: splitUrl(char ***tmp, int max_length, char *url)
{
 int idx=0;
 char * p;
 int i;

 p = strtok (url,"/");
 while (p != NULL && idx < max_length)
  {

   for (i=0;i<maxUrlSize-1 && p[i] != '\0';i++)
    (*tmp)[idx][i] = p[i];
   for ( ; i< maxUrlSize-1;i++)
    (*tmp)[idx][i] = '\0';

   printf("tmp[idx[%d]] %s\n",idx,(*tmp)[idx]);

   idx++;
   p = strtok (NULL, "/");
  }

 return idx;
};

printf("tmp[idx] ... 已正确打印。

但在我的 main 中运行该方法后:

  split_url = new char * [ maxUrlSplits ];
  for (int k=0;k<maxUrlSplits;k++)
   split_url[k] = new char [maxUrlSize];

  arr_size = crawl->splitUrl(&split_url,maxUrlSplits,url);

数组 split_url 为空。

有人

有想法吗?

I used strtok to split a string.

[UPDATE] I used your comments and answer for the new version below, but didn't work atm

int Crawl :: splitUrl(char ***tmp, int max_length, char *url)
{
 int idx=0;
 char * p;
 int i;

 p = strtok (url,"/");
 while (p != NULL && idx < max_length)
  {

   for (i=0;i<maxUrlSize-1 && p[i] != '\0';i++)
    (*tmp)[idx][i] = p[i];
   for ( ; i< maxUrlSize-1;i++)
    (*tmp)[idx][i] = '\0';

   printf("tmp[idx[%d]] %s\n",idx,(*tmp)[idx]);

   idx++;
   p = strtok (NULL, "/");
  }

 return idx;
};

The printf("tmp[idx] ... is correctly printed.

But in my main after I run the method:

  split_url = new char * [ maxUrlSplits ];
  for (int k=0;k<maxUrlSplits;k++)
   split_url[k] = new char [maxUrlSize];

  arr_size = crawl->splitUrl(&split_url,maxUrlSplits,url);

the array split_url is empty.

Compiler and gdb are fine.

Does someone have an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幽梦紫曦~ 2024-11-11 11:53:38

由于您没有更改指针指向的位置,因此您只需将 char * 传递给函数即可。所以

int Crawl :: splitUrl(char *tmp, int max_length, char *url)

Seeing as you are not changing where the pointer is pointing, you only need to pass in a char * to your function. So

int Crawl :: splitUrl(char *tmp, int max_length, char *url)
菩提树下叶撕阳。 2024-11-11 11:53:38
for (i=0;i<maxUrlSize || p[i] != '\0';i++)
    tmp[idx][i] = p[i];

这个 for 循环不可能是正确的。只要任一条件为真,您就复制字节。我相信只有当两者都成立时您才应该复制。

for (i=0;i<maxUrlSize || p[i] != '\0';i++)
    tmp[idx][i] = p[i];

This for loop can't be correct. You copy bytes a long as either condition is true. I believe you should copy only when both are true.

愁以何悠 2024-11-11 11:53:38

是的,这是正确的。
C 中的每个参数都可以按值调用,如果需要修改(填充数组),则应该使用指针。

       int Crawl :: splitUrl(char ***tmp, int max_length, char *url)
        {
         int idx=0;
         char * p;
         int i;

         p = strtok (url,"/");
         while (p != NULL)
          {

           for (i=0;i<maxUrlSize || p[i] != '\0';i++)
            (*tmp)[idx][i] = p[i];
           for ( ; i< maxUrlSize-1;i++)
            (*tmp)[idx][i] = '\0';

           printf("tmp[idx] %s\n",(*tmp)[idx]);

           idx++;
           p = strtok (NULL, "/");
          }


         return idx;
        };

...
      arr_size = crawl->splitUrl(&split_url,maxUrlSplits,url);

Yes, this is correct.
Every Parameter in C works as call by value, if you need a modification (fill your array), you should use a pointer.

       int Crawl :: splitUrl(char ***tmp, int max_length, char *url)
        {
         int idx=0;
         char * p;
         int i;

         p = strtok (url,"/");
         while (p != NULL)
          {

           for (i=0;i<maxUrlSize || p[i] != '\0';i++)
            (*tmp)[idx][i] = p[i];
           for ( ; i< maxUrlSize-1;i++)
            (*tmp)[idx][i] = '\0';

           printf("tmp[idx] %s\n",(*tmp)[idx]);

           idx++;
           p = strtok (NULL, "/");
          }


         return idx;
        };

...
      arr_size = crawl->splitUrl(&split_url,maxUrlSplits,url);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文