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

问题描述

我有一个const char *变量,它可能有一个像OpenStack:OpenStack1的值。我想标记这个const char *使用strtok其中delimiter(是一个const char *类型)是:。但是问题是strtok是以下类型:
char * strtok(char * str,const char * delimiters);

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 *。

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实际上是写入你的字符串,你需要把它的一个可写的副本tokenize;

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 *进行标记化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 04:55