struct_type[1] 是什么意思?
What does struct_type[1] mean?
我找到了一些获取结构大小的代码,如下所示:
sizeof(struct struct_type[1]);
我测试了 return struct_type
的大小。
和
sizeof(struct struct_type[2]);
returns 结构大小的两倍。
编辑:
struct_type
是结构,不是数组:
struct struct_type {
int a;
int b;
};
struct_type[1]
到底是什么意思?
记住sizeof
语法:
sizeof ( typename );
这里的 typename 是 struct struct_type[N]
或更具可读性的形式 struct struct_type [N]
,它是 N 个类型为 struct struct_type 的对象的数组。如您所知,数组大小是一个元素的大小乘以元素总数。
就像:
sizeof(int[1]); // will return the size of 1 int
和
sizeof(int[2]); // will return the size of 2 ints
也是:
sizeof(struct struct_type[1]); // return size of 1 `struct struct_type'
和
sizeof(struct struct_type[2]); // return size of 2 `struct struct_type'
这里struct struct_type[1]
和struct struct_type[2]
简单地表示struct struct_type
类型元素的arrays
,而sizeof
只是返回所表示的元素的大小数组。
为申报
int arr[10];
数组的大小可以通过使用 arr
作为操作数或 int [10]
来计算。由于 sizeof
运算符根据操作数的类型产生大小,因此 sizeof(arr)
和 sizeof (int [10])
都将 return 数组 arr
的大小(最终 arr
的类型是 int [10]
)。
C11-§6.5.3.3/2:
The sizeof operator yields the size (in bytes) of its operand, which may be an
expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
类似地,对于 struct struct_type
的数组
struct struct_type a[1];
大小可以通过sizeof (a)
或sizeof(struct struct_type[1])
计算得出。
我找到了一些获取结构大小的代码,如下所示:
sizeof(struct struct_type[1]);
我测试了 return struct_type
的大小。
和
sizeof(struct struct_type[2]);
returns 结构大小的两倍。
编辑:
struct_type
是结构,不是数组:
struct struct_type {
int a;
int b;
};
struct_type[1]
到底是什么意思?
记住sizeof
语法:
sizeof ( typename );
这里的 typename 是 struct struct_type[N]
或更具可读性的形式 struct struct_type [N]
,它是 N 个类型为 struct struct_type 的对象的数组。如您所知,数组大小是一个元素的大小乘以元素总数。
就像:
sizeof(int[1]); // will return the size of 1 int
和
sizeof(int[2]); // will return the size of 2 ints
也是:
sizeof(struct struct_type[1]); // return size of 1 `struct struct_type'
和
sizeof(struct struct_type[2]); // return size of 2 `struct struct_type'
这里struct struct_type[1]
和struct struct_type[2]
简单地表示struct struct_type
类型元素的arrays
,而sizeof
只是返回所表示的元素的大小数组。
为申报
int arr[10];
数组的大小可以通过使用 arr
作为操作数或 int [10]
来计算。由于 sizeof
运算符根据操作数的类型产生大小,因此 sizeof(arr)
和 sizeof (int [10])
都将 return 数组 arr
的大小(最终 arr
的类型是 int [10]
)。
C11-§6.5.3.3/2:
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
类似地,对于 struct struct_type
struct struct_type a[1];
大小可以通过sizeof (a)
或sizeof(struct struct_type[1])
计算得出。