linux 内核源代码分析 - 获取数组的大小
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
測试程序:
#include<stdio.h> #include<stdlib.h> struct dev { int a; char b; float c; }; struct dev devs[]= { { 1,'a',7.0, }, { 1,'a',7.0, }, { 1,'a',7.0, }, }; int main() { printf("int is %d \n" ,sizeof(int)); printf("char is %d\n",sizeof(char)); printf("float is %d\n",sizeof(float)); printf("devs is %d \n",sizeof(devs)); printf("devs[0] is %d\n",sizeof(devs[0])); printf("num is %d \n",sizeof(devs)/sizeof(devs[0])); return 0; }
实践结果例如以下: