C语言 查找字符数组的长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6150258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:46:48  来源:igfitidea点击:

Finding length of char array

carrays

提问by Nikhil

I have some code as follows:

我有一些代码如下:

int i=0;
char a[7]={0x00,0xdc,0x01,0x04};
int len=0;
len = sizeof(a);
printf("The Length is : %d", len);

Here I want to find the length of the array ain c? How can this be done?

这里我想a在c中找到数组的长度?如何才能做到这一点?

回答by Joe

By convention C strings are 'null-terminated'. That means that there's an extra byte at the end with the value of zero (0x00). Any function that does something with a string (like printf) will consider a string to end when it finds null. This also means that if your string is not null terminated, it will keep going until it finds a null character, which can produce some interesting results!

按照约定,C 字符串是“以空字符结尾的”。这意味着末尾有一个额外的字节,其值为零 ( 0x00)。任何对字符串(如printf)执行某些操作的函数在找到 null 时都会考虑以字符串结尾。这也意味着如果你的字符串不是空终止的,它会一直运行直到找到一个空字符,这会产生一些有趣的结果!

As the first item in your array is 0x00, it will be considered to be length zero (no characters).

由于数组中的第一项是 0x00,因此它将被视为长度为零(无字符)。

If you defined your string to be:

如果您将字符串定义为:

char a[7]={0xdc,0x01,0x04,0x00};

e.g. null-terminated

例如空终止

then you can use strlento measure the length of the string stored in the array.

那么您可以使用strlen来测量存储在数组中的字符串的长度。

sizeofmeasures the size of a type. It is not what you want. Also remember that the string in an array may be shorter than the size of the array.

sizeof测量类型的大小。这不是你想要的。还要记住,数组中的字符串可能比数组的大小短。

回答by lx.

sizeofreturns the size of the type of the variable in bytes. So in your case it's returning the size of your char[7]which is 7 * sizeof(char). Since sizeof(char) = 1, the result is 7.

sizeof以字节为单位返回变量类型的大小。因此,在您的情况下,它返回的char[7]7 * sizeof(char). 因为sizeof(char) = 1,结果是 7。

Expanding this to another example:

将此扩展为另一个示例:

int intArr[5];
printf("sizeof(intArr)=%u", sizeof(intArr));

would yield 5 * sizeof(int), so you'd get the result "20" (At least on a regular 32bit platform. On others sizeof(int)might differ)

会 yield 5 * sizeof(int),所以你会得到结果“20”(至少在常规的 32 位平台上。在其他平台上sizeof(int)可能会有所不同)

To return to your problem:

回到你的问题:

It seems like, that what you want to know is the length of the string which is contained inside your array and not the total array size.

看起来,您想知道的是数组中包含的字符串的长度,而不是数组的总大小。

By definition C-Strings have to be terminated with a trailing '\0' (0-Byte). So to get the appropriate length of the string contained within your array, you have to first terminate the string, so that you can tell when it's finished. Otherwise there would be now way to know.

根据定义,C 字符串必须以尾随的 '\0'(0 字节)结尾。因此,要获得数组中包含的字符串的适当长度,您必须首先终止该字符串,以便您知道它何时完成。否则现在就有办法知道了。

All standard functions build upon this definition, so if you call strlento retrieve the string length, it will iterate through the given array until it finds the first 0-byte, which in your case would be the very first element.

所有标准功能建立在这个定义,因此,如果调用strlen来检索STR荷兰国际集团LENGTH,它会通过给定的数组迭代,直到它找到第一个0字节,而你的情况将是第一个元素。

Another thing you might need to know that only because you don't fill the remaining elements of your char[7]with a value, they actually do contain random undefined values.

您可能需要知道的另一件事是,仅因为您没有char[7]用值填充其余元素,它们实际上确实包含随机未定义值。

Hope that helped.

希望有所帮助。

回答by Prince John Wesley

If you are expecting 4as output then try this:

如果您期望4作为输出,请尝试以下操作:

char a[]={0x00,0xdc,0x01,0x04};

字符a[]={0x00,0xdc,0x01,0x04};

回答by andrew

You can try this:

你可以试试这个:

   char tab[7]={'a','b','t','u','a','y','t'};
   printf("%d\n",sizeof(tab)/sizeof(tab[0]));

回答by Mayank

You can do len = sizeof(a)/sizeof(*a)for any kind of array. But, you have initialized it as a[7] = {...}meaning its length is 7...

你可以len = sizeof(a)/sizeof(*a)为任何类型的数组做。但是,您已将其初始化为a[7] = {...}意味着其长度为 7 ...

回答by IT_Dewd

If anyone is looking for a quick fix for this, here's how you do it.

如果有人正在为此寻找快速解决方案,请按以下步骤操作。

while (array[i] != '##代码##') i++;

The variable iwill hold the used length of the array, not the entire initialized array. I know it's a late post, but it may help someone.

变量i将保存数组的使用长度,而不是整个初始化数组。我知道这是一个迟到的帖子,但它可能会帮助某人。