关于字符串长度,终止NUL等 [英] About string length, terminating NUL, etc

查看:83
本文介绍了关于字符串长度,终止NUL等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习C,我对char数组和字符串之间的差异以及它们的工作方式感到困惑.

I'm currently learning C and I'm confused with differences between char array and string, as well as how they work.

问题1:

为什么源代码1和源代码2的结果有所不同?

Why is there a difference in the outcomes of source code 1 and source code 2?

源代码1:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char c[2]="Hi";
    printf("%d\n", strlen(c));   //returns 3 (not 2!?)
    return 0;
}

源代码2:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char c[3]="Hi";
    printf("%d\n", strlen(c));   //returns 2 (not 3!?)
    return 0;
}

问题2:

字符串变量与char数组有何不同?如何用最小的索引号声明它们以允许存储\ 0(如果有)(请阅读下面的代码)?

How is a string variable different from a char array? How to declare them with the minimum required index numbers allowing \0 to be stored if any (please read the codes below)?

char name[index] = "Mick";   //should index be 4 or 5?

char name[index] = {'M', 'i', 'c', 'k'};   //should index be 4 or 5?

#define name "Mick"   //what is the size? Is there a \0?

问题3:

终止NUL仅跟随字符串而不跟随char数组吗?那么字符串"Hi"的实际值是[H] [i] [\ 0],而char数组"Hi"的实际值是[H] [i]?

Does the terminating NUL ONLY follow strings but not char arrays? So the actual value of the string "Hi" is [H][i][\0] and the actual value of the char array "Hi" is [H][i]?

问题4:

假设c [2]将存储"Hi",后跟一个\ 0(不确定如何执行此操作,也许使用gets(c)?).那么\ 0存放在哪里?是将其存储在c [2]之后的某处"成为[H] [i] \ 0,还是将c [2]附加一个\ 0成为c [3],即[H] [i] [\ 0 ]?

Suppose c[2] is going to store "Hi" followed by a \0 (not sure how this is done, using gets(c) maybe?). So where is the \0 stored? Is it stored "somewhere" after c[2] to become [H][i]\0 or will c[2] be appended with a \0 to become c[3] which is [H][i][\0]?

令人困惑的是,有时在字符串/字符数组之后有一个\ 0,当我用if (c1==c2)比较两个变量时会引起麻烦,因为它很可能返回FALSE(0).

It is quite confusing that sometimes there is a \0 following the string/char array and causes trouble when I compare two variables by if (c1==c2) as it most likely returns FALSE (0).

详细的答案将不胜感激.但是简短的回答有助于我的理解:) 预先谢谢你!

Detailed answers are appreciated. But keeping your answer brief helps my understanding :) Thank you in advance!

推荐答案

问题1:

为什么源代码1和源代码的结果有所不同 代码2?

Why is there a difference in the outcomes of source code 1 and source code 2?

源代码1:

#include <stdio.h>
#include <string.h>

int main()
{
    char c[2]="Hi";
    printf("%d", strlen(c));   //returns 3 (not 2!?)
    getchar();
}

源代码2:

Source code 2:

#include <stdio.h>
#include <string.h>

int main()
{
    char c[3]="Hi";
    printf("%d", strlen(c));   //returns 2 (not 3!?)
    getchar();
}

answer : 因为在第一种情况下,c[]仅保留"Hi". strlen末尾查找零,并且根据c[]后面的确切含义,迟早会发现一个零,否则会崩溃.我们不能不确切知道c[]数组后面的内存中的内容而说.

answer: Because in the first case, c[] is only holding "Hi". strlen looks for a zero at the end, and, depending on exactly what is behind c[] finds one sooner or later, or crashes. We can't say without knowing exactly what is in the memory behind the c[] array.

问题2:

字符串变量与char数组有何不同?如何申报 它们具有所需的最小索引号,从而可以存储\ 0 如果有的话(请阅读下面的代码)?

How is a string variable different from a char array? How to declare them with the minimum required index numbers allowing \0 to be stored if any (please read the codes below)?

char name[index] = "Mick";   //should index be 4 or 5?

char name[index] = {'M', 'i', 'c', 'k'};   //should index be 4 or 5?

answer 真的取决于您要做什么.如果您想实际使用内容作为字符串,则可能为5.但是,没有什么可以说不能将"Mick"存储在4个字符的数组中的-您只能使用strlen来找出它的长度,因为strlen会持续到5,甚至很可能(很远)才能找到长度,并且如果接下来的几个内存位置中没有零,则可能会导致崩溃,因为最终,将没有有效的内存地址可供读取.

answer Really depends on what you want to do. Probably 5 if you want to actually use the content as a string. But there's nothing saying you can't store "Mick" in a 4 character array - you just can't use strlen to find out how long it is, because strlen will continue to 5 and quite possibly (much) further to find the length, and if there is no zero in the next several memory locations, it could lead to a crash, because eventually, there won't be valid memory addresses to read.

#define name "Mick" //what is the size? Is there a \0?

在您使用名称somwhere之前,这绝对没有大小. #define不是编译器看到的内容-如果在任何地方使用name,预处理器都将name替换为"Mick"-希望这是编译器可以理解的地方.然后,应用与上一个答案相同的规则-这取决于您要如何使用字符数组.为了正确使用strlenstrpy和几乎所有其他str...函数,最后需要一个零.

This has absolutely no size at all, until you use name somwhere. #defines are not part of what the compiler sees - the pre-processor will replace name with "Mick" if you use name anywhere - and hopefully, that's in a place the compiler can make sense of. And then the same rules apply as in previous answer - it depends on how you want to use the array of characters. For correct operation with strlen, strpy, and nearly all other str... functions, you need a zero at the end.

问题3:

终止的null是否仅跟随字符串而不是char数组?所以 字符串"Hi"的实际值是[H] [i] [\ 0],实际值 字符数组"Hi"的值是[H] [i]?

Does the terminating null ONLY follow strings but not char arrays? So the actual value of the string "Hi" is [H][i][\0] and the actual value of the char array "Hi" is [H][i]?

是,不,也许.这完全取决于您如何使用"Hi"字符串文字(这是双引号内的内容"的技术名称).如果编译器被允许",它将在末尾加零.但是,如果将数组初始化为给定的大小,它将在其中填充字节,并且如果没有空间可以容纳零,那是您的问题,而不是编译器的问题.

Yes, no, maybe. It all depends on how you USE the "Hi" string literal (that's the technical name for 'something within double quotes'). If the compiler is "allowed", it will put a zero at the end. But if you initialize an array to a given size, it will stuff the bytes in there, and if there isn't room for a zero, that's your problem, not the compiler's.

问题4:

假设c [2]将存储"Hi",后跟\ 0(不确定如何 这可以通过使用gets(c)完成吗?).那么\ 0存放在哪里?是吗 存储在c [2]之后的某处"成为[H] [i] \ 0或将c [2]设为 附加\ 0成为c [3],即[H] [i] [\ 0]?

Suppose c[2] is going to store "Hi" followed by a \0 (not sure how this is done, using gets(c) maybe?). So where is the \0 stored? Is it stored "somewhere" after c[2] to become [H][i]\0 or will c[2] be appended with a \0 to become c[3] which is [H][i][\0]?

在c [2]中,除了"H","i"之外,没有告诉我们存储了什么[从技术上讲,很可能是地球的尽头"-用计算机的术语来说,这是可以不会被读取-在这种情况下,strlen将会使您的程序崩溃,因为strlen的读取范围超出了地球的尽头."但也可以是零,一,字母"a",数字42或任何其他8位[1]值.

In c[2], beyond the 'H', 'i', there is no telling what is stored [technically, it could well be "the end of the earth" - in computer terms, that's "memory that can't be read - in which case strlen on that WILL crash your program, because strlen reads beyond the end of the earth]. But if could also be a zero, a one, the letter 'a', the number 42, or any other 8-bit [1] value.

令人困惑的是,有时在\之后有一个\ 0 字符串/字符数组,当我比较两个变量时会引起麻烦 if(c1 == c2),因为它很可能返回FALSE(0).

It is quiet confusing that sometimes there is a \0 following the string/char array and causes trouble when I compare two variables by if (c1==c2) as it most likely returns FALSE (0).

如果c1和c2是char数组,则由于c1和c2永远不会具有相同的地址,所以它将始终为false,并且当以这种方式在C中使用数组时,它将变为第一个内存中的地址数组中的元素".因此,无论c1和c2的内容是什么,它们的地址都永远不会相同[因为它们是两个不同的变量,并且两个变量在内存中不能具有相同的位置-就像试图将两辆车停在一个停车位中一样仅可容纳一辆汽车就足够大了-不,在我们的思想实验中,不允许压碎任何一辆汽车].

If c1 and c2 are char arrays, that will ALWAYS be false since c1 and c2 are never going to have the same address, and when using an array in C in that way, it becomes "the address in memory of the first element in the array". So no matter what teh contents of c1 and c2 is, their address can never be the same [because they are two different variables, and two variables can not have the same location in memory - that's like trying to park two cars in a parking space large enough only for one car - and no, crushing either car is not allowed in our thought experiment].

[1]不保证Char为8位.但是现在让我们开始.

[1] Char isn't guaranteed to be 8 bits. But lets inore that for now.

这篇关于关于字符串长度,终止NUL等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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