本教程操作环境:windows7系统、c99版本、dell g3电脑。
相关推荐:c语言视频教程
c语言中计算一个数组占内存多少空间(字节数)
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;}
更多编程相关知识,请访问:编程教学!!
以上就是c语言中数组所占字节怎么算的详细内容。
