感觉是strtok_r的问题

mznxbc123456 2016-07-11 05:38:12
遇到一个问题,请大侠帮忙看看,我不知道是哪里出错了
char IPPORT[25]={0};
const char* ss="192.168.1.1:5533";
strcpy(IPPORT,ss);
int in=0;
char *p[2]={0};
char c_port[]="80";//默认80端口
char *buf=IPPORT;
INFO("BUF=%s",buf);
char *outer_ptr=NULL;
int port=0;
char *ip;
char *ps;
ps=strchr(buf,':');
if(ps){
INFO("PS");
while((p[in] = strtok_r(buf,":",&outer_ptr))!=NULL){
in++;
buf=NULL;
}
if(p[1]==NULL){
INFO("IP IS NUMM");
p[1]=c_port;
}
port=atoi(p[1]);
ip=p[0];
printf("p[0]=%s,p[1]=%s\n",p[0],p[1]);
}
else{
INFO("###################");
ip=buf;
port=80;
}
INFO("IP=%s,PORT=%d",ip,port);

我的目的就是想切分ss 192.168.1.1:5533
IP=192.168.1.1 PORT=5533
我感觉代码没问题 但没正确输出啊
p[0]是空,求大侠解释
...全文
159 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
精分患者 2016-07-11
  • 打赏
  • 举报
回复
把 char *p[2] = {0} 改成 char *p[3] = {0}

23,110

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧