sizeof 运算符 (C)sizeof Operator (C)

sizeof 运算符提供了存储操作数的类型的对象所需的存储空间量(以字节为单位)。The sizeof operator gives the amount of storage, in bytes, required to store an object of the type of the operand. 利用此运算符,你可以避免在程序中指定依赖于计算机的数据大小。This operator allows you to avoid specifying machine-dependent data sizes in your programs.

语法Syntax

sizeof unary-expression  
sizeof ( type-name )  

备注Remarks

操作数是作为 unary-expression 或 type-cast 表达式的标识符(即,用括号括起的类型说明符)。The operand is either an identifier that is a unary-expression, or a type-cast expression (that is, a type specifier enclosed in parentheses). unary-expression 不能表示位域对象、不完整类型或函数指示符。The unary-expression cannot represent a bit-field object, an incomplete type, or a function designator. 结果是一个无符号整数常量。The result is an unsigned integral constant. 标准标头 STDDEF.H 将此类型定义为 size_tThe standard header STDDEF.H defines this type as size_t.

在将 sizeof 运算符应用于数组标识符时,结果是整个数组的大小而不是由数组标识符表示的指针的大小。When you apply the sizeof operator to an array identifier, the result is the size of the entire array rather than the size of the pointer represented by the array identifier.

sizeof 运算符应用于结构或联合类型名称,或者应用于结构或联合类型的标识符时,结果是结构或联合中的字节数(包括内部和尾部填充)。When you apply the sizeof operator to a structure or union type name, or to an identifier of structure or union type, the result is the number of bytes in the structure or union, including internal and trailing padding. 此大小可能包括用于在内存边界上对齐结构成员或联合成员的内部和尾部填充。This size may include internal and trailing padding used to align the members of the structure or union on memory boundaries. 因此,结果可能不对应于通过将各个成员的存储需求相加计算出的大小。Thus, the result may not correspond to the size calculated by adding up the storage requirements of the individual members.

如果某个未确定大小的数组是结构的最后一个元素,则 sizeof 运算符将返回没有该数组的结构的大小。If an unsized array is the last element of a structure, the sizeof operator returns the size of the structure without the array.

buffer = calloc(100, sizeof (int) );  

此示例使用 sizeof 运算符传递 int 的大小(因计算机而异)作为名为 calloc 的运行时函数的参数。This example uses the sizeof operator to pass the size of an int, which varies among machines, as an argument to a run-time function named calloc. 该函数返回的值存储在 buffer 中。The value returned by the function is stored in buffer.

static char *strings[] = {  
      "this is string one",  
      "this is string two",  
      "this is string three",  
   };  
const int string_no = ( sizeof strings ) / ( sizeof strings[0] );   

在此示例中,strings 是指向 char 的指针的数组。In this example, strings is an array of pointers to char. 指针的数目是数组中元素的数目,但是未指定。The number of pointers is the number of elements in the array, but is not specified. 通过使用 sizeof 运算符计算数组中元素的数目,可以轻松确定指针数目。It is easy to determine the number of pointers by using the sizeof operator to calculate the number of elements in the array. const 整数值 string_no 将初始化为此数字。The const integer value string_no is initialized to this number. 由于它是 const 值,因此 string_no 无法修改。Because it is a const value, string_no cannot be modified.

另请参阅See Also

C 运算符C Operators
C++ 内置运算符、优先级和关联性C++ Built-in Operators, Precedence and Associativity