C中获取数组元素个数的骚操作

Java中数组元素个数的获取

在java中想要获取数组的元素个数很简单,只需要调用数组类的length方法,比如

public static void main(string args[])
{ 
	int a[5]={ 1,2,3,4,5);
	System.out.println("The length of array a is %d",a.length);
}

但在C中没有直接获取数组长度的方法

C中数组元素个数的获取

可以用sizeof函数来获取数组占用内存的长度,然后除以单个元素的长度,就是数组中元素个数,即

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int main()
{ 
	int a[SIZE]={ 1,2,3,4,5};
	//用sizeof函数来获取数组占用内存的长度,然后除以单个元素的长度
	int length=sizeof(a)/sizeof(int);	
	printf("The length of array a is %d\n",length);
	system("pause");
	return 0;
}
    原文作者:奋豆者
    原文地址: https://blog.csdn.net/qq_36981023/article/details/88763226
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞