当前位置: 技术问答>linux和unix
C程序输出结果为什么不是 100*12 ??
来源: 互联网 发布时间:2016-12-25
本文导语: #include #include #include typedef struct st_type { int i; int a[2]; }type_a; int main() { type_a *p = (type_a*)malloc(100*sizeof(type_a)); printf("sizeof(*p) == %dn",sizeof(*p)); //为什么运行结果为12 ??????? return 0; } ...
#include
#include
#include
typedef struct st_type
{
int i;
int a[2];
}type_a;
int main()
{
type_a *p = (type_a*)malloc(100*sizeof(type_a));
printf("sizeof(*p) == %dn",sizeof(*p)); //为什么运行结果为12 ???????
return 0;
}
#include
#include
typedef struct st_type
{
int i;
int a[2];
}type_a;
int main()
{
type_a *p = (type_a*)malloc(100*sizeof(type_a));
printf("sizeof(*p) == %dn",sizeof(*p)); //为什么运行结果为12 ???????
return 0;
}
|
http://en.wikipedia.org/wiki/Sizeof
The sizeof operator is used to determine the amount of space any data-element/datatype occupies in memory. To use sizeof, the keyword "sizeof" is followed by a type name, variable, or expression. If a type name is used, it always needs to be enclosed in parentheses, whereas variable names and expressions can be specified with or without parentheses. A sizeof expression evaluates to an unsigned size_t value equal to the size in bytes of the "argument" datatype, variable, or expression (with datatypes, sizeof evaluates to the size of the datatype; for variables and expressions it evaluates to the size of the type of the variable or expression).
注意红色的部分,因为*p被是个变量,所以sizeof会先计算出这个变量所对应类型的size,也就是type_a的size
The sizeof operator is used to determine the amount of space any data-element/datatype occupies in memory. To use sizeof, the keyword "sizeof" is followed by a type name, variable, or expression. If a type name is used, it always needs to be enclosed in parentheses, whereas variable names and expressions can be specified with or without parentheses. A sizeof expression evaluates to an unsigned size_t value equal to the size in bytes of the "argument" datatype, variable, or expression (with datatypes, sizeof evaluates to the size of the datatype; for variables and expressions it evaluates to the size of the type of the variable or expression).
注意红色的部分,因为*p被是个变量,所以sizeof会先计算出这个变量所对应类型的size,也就是type_a的size