简体   繁体   English

sizeof()运算符的行为

[英]Behaviour of sizeof() operator

For the following piece of code: 对于以下代码:

char a[] = "Apple";
char *s[] = {"Apple"};
printf("%d %d\n", sizeof(a), sizeof(s[0]));

The output is: 输出为:

6 4

Can someone tell me why sizeof() is giving different outputs? 谁能告诉我为什么sizeof()提供不同的输出?

EDIT: I did intend to type sizeof() originally, but typed strlen() instead. 编辑:我原本打算键入sizeof() ,但改为键入strlen() I apologize for the same. 我对此表示歉意。 I have edited the statement now. 我已经编辑了该声明。

sizeof给你的数字char小号分配到a ,而strlen为您提供了可用的字符串的长度在a

sizeof(a) is the size of the array, which contains the terminator as well as the 5 printable characters. sizeof(a)是数组的大小,其中包含终止符以及5个可打印字符。

strlen(s[0]) gives the length of the string, excluding the terminator, since that is what strlen is specified to do. strlen(s[0])给出字符串的长度,不包括终止符,因为这是strlen被指定要做的事情。

UPDATE: sizeof(s[0]) is the size of a pointer. 更新: sizeof(s[0])是指针的大小。 There's no way to determine the size of an array given just a pointer to it. 仅通过指向数组的指针就无法确定数组的大小。

\\0算作字符串内存大小的一部分,但是字符串本身的长度strlen()strlen()给出strlen()仅由遇到\\0之前的字符给出。

A character array declared like this: 声明如下的字符数组:

char a[] = "Apple";

has, according to the language specification, a null-terminator. 根据语言规范,它具有一个空终止符。 Therefore, the length of the array is 6. There are 5 characters, and then the null terminator. 因此,数组的长度为6。有5个字符,然后是空终止符。

On the other hand, strlen() returns the number of characters that precede the null terminator. 另一方面, strlen()返回空终止符之前的字符数。 Which is 5. 是5

The sizeof operator yields the size (in bytes) of its operand. sizeof运算符产生其操作数的大小(以字节为单位)。

In this statement 在此声明中

char a[] = "Apple";

array a is initialized by characters of string literal "Apple" that includes the terminating zero. 数组a由字符串文字“ Apple”的字符初始化,该字符包括结尾的零。 In fact this record is equivalent to 实际上,这个记录相当于

char a[] = { 'A', 'p', 'p', 'l', 'e', '\0'; };

So the size in bytes of a is equal to 6. 因此,a的字节大小等于6。

Standard C function strlen counts symbols in a string until it encounters the terminating zero. 标准的C函数strlen对字符串中的符号进行计数,直到遇到终止的零为止。 So 所以

strlen( a ) strlen(a)

will return 5 that is the number of characters in the array that are before the terminating zero. 将返回5,即数组中终止于零之前的字符数。

Take into account that you could write for example 考虑到您可以编写例如

char a[100] = "Apple";

In this case sizeof( a ) will yield 100 because you explicitly specified the number of bytes that the array will occupy. 在这种情况下, sizeof( a )将产生100,因为您明确指定了数组将占用的字节数。 However it was initialized only with 6 characters of the string literal. 但是,它仅用字符串文字的6个字符初始化。 So how to find how many actual data are in the character array? 那么如何查找字符数组中有多少实际数据呢? For this purpose function strlen was introduced that to distinguish the size of a character array and the number of actual data in the character array. 为此,引入了strlen函数,以区分字符数组的大小和字符数组中实际数据的数量。

Because in C there is no string type. 因为在C中没有字符串类型。 String is a character array which is NULL terminated. 字符串是一个以NULL终止的字符数组。 strlen() counts the characters until the NULL character, whereas sizeof() actually returns the amount of memory used up by the charater array. strlen()对字符进行计数,直到NULL字符为止,而sizeof()实际上返回charater数组使用的内存量。

a is an array of char s, which contains 6 elements, hence sizeof returns 6 (the length of the string including zero termination). achar数组,其中包含6个元素,因此sizeof返回6(字符串的长度,包括零终止)。

s is an array of pointers to char . s是一个指向char的指针数组。 The pointer size is 4 bytes. 指针大小为4个字节。 sizeof(s[0]) returns the size of the first element, which is pointer, ie its size is 4. sizeof(s[0])返回第一个元素的大小,即指针,即其大小为4。

When you define: 定义时:

char a[] = "Apple"; char a [] =“ Apple”;

It means an array of characters, which equals to the following definition: 它表示一个字符数组,它等于以下定义:

char a[] = {'A', 'p', 'p', 'l', 'e', '\\0'}; char a [] = {'A','p','p','l','e','\\ 0'}; // '\\0' is the string termination character which equals to 0 //'\\ 0'是等于0的字符串终止符

Since char type size is 1, the sizeof(a) returns 6 which is the size of the whole array. 由于char类型的大小为1,因此sizeof(a)返回6,即整个数组的大小。

Nevertheless, when you define: 不过,在定义时:

char *s[] = {"Apple"}; char * s [] = {“ Apple”};

It means an array of char pointer . 它表示一个char指针数组。 Hence sizeof(s[0]) return the size of its first element which equals to sizeof(char*). 因此,sizeof(s [0])返回其第一个元素的大小,该大小等于sizeof(char *)。 For a 32-bit platform, sizeof(char*) = 4. If you do it on an 64-bit platform, 8 is the expected value. 对于32位平台,sizeof(char *)=4。如果在64位平台上执行,则期望值为8。

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

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