简体   繁体   English

检查非空终止字符串的有效性

[英]Checking validity of non null-terminated string

I am having trouble wrapping my brain around null terminators and non-null terminating arrays. 我无法绕着null终止符和非null终止数组包裹我的大脑。

Let's say I have two declarations: 假设我有两个声明:

const char *string = "mike";

and

const char string[4] = {'m', 'i', 'k', 'e'};

I understand the first declaration is because in C, a character array is null terminated because it is a defined to be a contiguous block of characters in memory terminated by a NULL and I can check this with strlen . 我理解第一个声明是因为在C中,字符数组是空终止的,因为它被定义为内存中以NULL结尾的连续字符块,我可以用strlen来检查它。

The problem that I'm having is understanding declarations like the second, with no null terminator. 我遇到的问题是理解第二个声明,没有空终止符。

How can I check for validity of a string with no null terminator? 如何检查没有空终止符的字符串的有效性? (as in, what if there are additional values in the array?) (如果在数组中有其他值,该怎么办?)

You can't. 你不能。 A string with no null terminator is not a string. 没有null终止符的字符串不是字符串。 It's just an array of characters. 它只是一个字符数组。 AC string must have a null terminator to be considered a string. AC字符串必须具有空终止符才能被视为字符串。

You'd have to deal with it like you would with an int[] array or any other type of array: keep track of the size separately, if it's not a known fixed size. 您必须像处理int[]数组或任何其他类型的数组一样处理它:如果它不是已知的固定大小,请分别跟踪大小。 Since it's not a string, you couldn't call string functions like strlen . 因为它不是字符串,所以你不能像strlen那样调用字符串函数。

How can I check for validity of a string with no null terminator? 如何检查没有空终止符的字符串的有效性?

You need to know array bounds in order to see if a null-terminated string is contained within the bounds. 您需要知道数组边界,以查看边界内是否包含以null结尾的字符串。 Here is how you can do that in your example: 以下是您在示例中如何做到这一点:

const char string[4] = {'m', 'i', 'k', 'e'};
int good = 0;
for (int i = 0 ; i != sizeof(string) ; i++) {
    if (string[i] == '\0') {
        good = 1;
        break;
    }
}
if (good) {
    printf("String '%s' is null-terminated.\n", string);
} else {
    printf("String is not null-terminated; cannot print.\n");
}

Although C library provides support only for null-terminated strings, you could use character arrays without null termination as long as you have access to their size (ie it's an array, not a pointer). 尽管C库仅支持以null结尾的字符串,但只要您可以访问其大小(即它是数组,而不是指针),您就可以使用没有空终止的字符数组。 For example, you could print your array like this: 例如,您可以像这样打印数组:

printf("'%.*s'\n", sizeof(string), string);

A string needs to end will a null terminator. 字符串需要结束将是一个空终止符。 If you tried to do printf("%s",string) on the second example or use functions like strcmp , strcpy , or strlen it would not work. 如果您尝试在第二个示例上执行printf("%s",string)或使用strcmpstrcpystrlen等函数,则无效。 It is true the a string is just an array of characters with a null terminator, but the null terminator needs to be there if is to be consider a string. 确实,字符串只是一个带有空终止符的字符数组,但是如果要考虑字符串,则需要使用空终止符。 So if you are not sure that you actually have an array of characters that is null terminated, you'll need to check for the null terminator. 因此,如果您不确定实际上是否有一个空终止的字符数组,则需要检查空终止符。

This distinction is very important, but the similarities can be used to your advantage especially when you get to the embedded level of programming and are reviving characters across a wire. 这种区别非常重要,但是相似之处可以用于您的优势,特别是当您进入嵌入式编程并通过线路恢复字符时。 Let's just say you have a buffer that you are putting revived characters in and you are looking for the string "mike". 我们只是说你有一个缓冲区,你将复活的字符放入,你正在寻找字符串“迈克”。 You most likely will not receive a null terminator across a wire so when you search for the string, you'll either need to compare the characters individually or use strncmp which only compares the number of characters that you tell it to which if you have a hardcoded string you can use strlen to get the size of that string you use for strncmp . 您很可能不会通过电线接收空终止符,因此当您搜索字符串时,您需要单独比较字符或使用strncmp ,它只比较您告诉它的字符数,如果您有硬编码字符串,您可以使用strlen来获取用于strncmp字符串的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM