如何使用strtok的标记化对一个const char *? [英] How to use Strtok for tokenizing a Const char*?

查看:144
本文介绍了如何使用strtok的标记化对一个const char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个const char *变量,它可能像一个值的OpenStack:OpenStack1。我想用的strtok所在的分隔符(这是一个const char *类型)是来标记这个为const char *:。但问题是strtok的是以下类型:
字符* strtok的(字符*海峡,为const char *分隔符);

I have a const char* variable which may have a value like "OpenStack:OpenStack1". I want to tokenize this const char* using strtok where the delimiter(which is of a const char* type) is ":" . But the problem is strtok is of following type: char * strtok ( char * str, const char * delimiters );

这意味着我不能用为const char *第一个输入的,因为它有是char *。你可以说我,我怎么能这样为const char *转换成char *?

Which means I can't use const char* for the first input as it has to be char*. Could you say me how I can convert this const char* into char*?

感谢您。

推荐答案

由于strtok的实际写入到您的字符串,你需要做一个写副本的它记号化;

Since strtok actually writes to your string, you need to make a writable copy of it to tokenize;

char* copy = strdup(myReadonlyString);
...tokenize copy...
free(copy);

这篇关于如何使用strtok的标记化对一个const char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆