将无符号char数组传递给字符串函数 [英] passing unsigned char array to string functions
问题描述
说我有一些utf8编码的字符串.在其中,使用";"
分隔单词.
但是此字符串中的每个字符(";"
除外)都具有 utf8值> 128 .
假设我将此字符串存储在unsigned char
数组中:
Say I have some utf8 encoded string. Inside it words are delimited using ";"
.
But each character (except ";"
) inside this string has utf8 value >128.
Say I store this string inside unsigned char
array:
unsigned char buff[]="someutf8string;separated;with;";
将此buff
传递给strtok
函数是否安全? (如果我只是想使用";"
符号提取单词).
Is it safe to pass this buff
to strtok
function? (If I just want to extracts words using ";"
symbol).
我担心的是strtok
(或者还有strcpy
)期望使用char
指针,但是在我的内部
字符串,某些值的值> 128.
这样的行为定义了吗?
My concern is that strtok
(or also strcpy
) expect char
pointers, but inside my
string some values will have value > 128.
So is this behaviour defined?
推荐答案
根据C11标准(ISO/IEC 9899:2011§7.24.1字符串处理约定,¶3,添加了重点):
According to the C11 Standard (ISO/IEC 9899:2011 §7.24.1 String Handling Conventions, ¶3, emphasis added):
对于本节中的所有功能,每个字符应为 解释为具有
unsigned char
类型(因此每个 可能的对象表示形式有效且具有不同的值.
For all functions in this subclause, each character shall be interpreted as if it had the type
unsigned char
(and therefore every possible object representation is valid and has a different value).
注意:C99标准中没有此段.
所以我没看到问题.
这篇关于将无符号char数组传递给字符串函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!