C++ strtok 无法获得 2 个代币 Borland
char *ParseCmdX(char *buf,int len)
{
char *p;
p = strtok(buf," ,");
p = strtok(NULL," ,");
char *ptr = (char *)malloc(strlen(p)+1);
strcpy(ptr,p);
return ptr;
}
为什么我在 p = strtok(NULL," ,");
处收到访问冲突错误? 当我输入 NULL 参数时,它会中断...
buf 类似于“das sdg hfg jgh”
char *ParseCmdX(char *buf,int len)
{
char *p;
p = strtok(buf," ,");
p = strtok(NULL," ,");
char *ptr = (char *)malloc(strlen(p)+1);
strcpy(ptr,p);
return ptr;
}
Why am I getting an Access Violation error at p = strtok(NULL," ,");
?
When I put NULL parameter it breaks ...
buf is smth like "das sdg hfg jgh"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是如何调用这个函数的,buf是如何分配的?请记住 buf 不能是只读的或常量(strtok 在调用时更改输入缓冲区)。
这是可行的,因为 x 是可变的:
但是,这会出现段错误:
How are you calling this function, and how is buf allocated? Remember buf cannot be read-only or const (strtok changes the input buffer as it's called).
This works, because x is changable:
This, however, seg faults:
您必须检查对 strtok 的第一次调用,即 strtok(but, " ,") 是否返回 NULL,如果是,则对 strtok 的下一次调用将终止。您具体传递给 buf 中的 ParseCmdX 的是什么?
You have to check and see if the first call to strtok, namely, strtok(but, " ,") returned NULL or not, if it did then the next call to strtok will die. what specifically are you passing to the ParseCmdX in buf?