strtok 和 strcpy 错误
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您没有更改指针指向的位置,因此您只需将 char * 传递给函数即可。所以
Seeing as you are not changing where the pointer is pointing, you only need to pass in a char * to your function. So
这个 for 循环不可能是正确的。只要任一条件为真,您就复制字节。我相信只有当两者都成立时您才应该复制。
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.
是的,这是正确的。
C 中的每个参数都可以按值调用,如果需要修改(填充数组),则应该使用指针。
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.