打印
[资料分享与下载]

strtok处理字符串常量

[复制链接]
564|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
東南博士|  楼主 | 2015-9-20 14:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
strtok()函数不能直接处理const char*, 用strncpy得到一个char型数组, 对这个temp数组进行处理即可。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main (void)
{
    char *string = "T01:91+:123";
    char tmp[16];
    char* s;

    /* Strtok can not do with const char*, so I use strncpy copy it to an char array */
    strncpy(tmp, string, sizeof(string));

    s = strtok (tmp, "T");
    printf("string: %s\n", string); // 01:91+:123
    s = strtok (s, ":");

    printf("city id: %s\n", s); // 01
    return 0;
}


相关帖子

沙发
cowboy2014| | 2015-9-20 20:34 | 只看该作者
将了好多C语言的知识啊

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

381

主题

6080

帖子

34

粉丝