C 语言中有一个专门用于检测类型或变量或数组在内存中所占有的空间 (字节数) 的操作符 sizeof, 用它可以直接检测出数组在内存占有的字节数.
语法规则是: sizeof(x);(识别没有歧义时也可写成 sizeof x;)-- 其中 x 是类型名, 变量名或数组名等, 返回 x 所占字节数(int 型).
示例:
- #include "stdio.h"
- struct X{
- int d;
- float t;
- double b;
- char n[100];
- };
- int main(int argc,char *argv[]){
- int a[]={1,2,3,4,5,6,7,8,9,10};
- double y=3.1415926;
- struct X t[3]={{0,0.0f,0.0,""},};// 结构体数组属复杂类型
- printf("10 elements of int array needs %d bytes.\n",sizeof a);// 检测整型数组
- printf("Double variables of type need %d bytes.\n",sizeof(y));//double 类型变量
- printf("Type float need %d bytes.\n",sizeof(float));//float 类型
- printf("Structure array't[3]'need %d bytes.\n",sizeof t);// 检测复杂类型
- return 0;
- }
更多相关知识请关注前端学习网站 https://www.html.cn/
来源: http://www.css88.com/qa/other/19029.html