c语言中获取数组的长度
001、一维数组
[root@PC1 test1]# ls test.c [root@PC1 test1]# cat test.c #include <stdio.h> int main(void) { int v1[5] = {3, 4, 8}; printf("length of v1 is %d\n", sizeof(v1)/sizeof(v1[0])); return 0; } [root@PC1 test1]# gcc test.c -o kkk [root@PC1 test1]# ls kkk test.c [root@PC1 test1]# ./kkk length of v1 is 5
。
[root@PC1 test1]# ls test.c [root@PC1 test1]# cat test.c #include <stdio.h> int main(void) { int v1[8]; printf("length of v1 is %d\n", sizeof(v1)/sizeof(v1[0])); return 0; } [root@PC1 test1]# gcc test.c -o kkk [root@PC1 test1]# ls kkk test.c [root@PC1 test1]# ./kkk length of v1 is 8
。
02、二维数组
[root@PC1 test1]# ls test.c [root@PC1 test1]# cat test.c #include <stdio.h> int main(void) { int v1[4][3] = {{4,3,2},{2,4,8},{0, 3, 4},{7,45,3}}; printf("length of v1 is %d\n", sizeof(v1)/sizeof(v1[0][0])); return 0; } [root@PC1 test1]# gcc test.c -o kkk [root@PC1 test1]# ls kkk test.c [root@PC1 test1]# ./kkk length of v1 is 12
。