How can I find the number of elements in an array?

前端 未结 14 1184
广开言路
广开言路 2020-12-02 15:01

I have an int array and I need to find the number of elements in it. I know it has something to do with sizeof but I\'m not sure how to use it exac

14条回答
  •  一向
    一向 (楼主)
    2020-12-02 15:38

    In real we can't count how many elements are store in array

    But u can find the array length or size using sizeof operator.

    But why we can't find how many elements are present in my array.

    Because when we initialise a array compiler give memory on our program like a[10] (10 blocks of 4 size ) and every block has garbage value if we put some value in some index like a[0]=1,a[1]=2,a[3]=8; and other block has garbage value no one can tell which value Is garbage and which value is not garbage that's a reason we cannot calculate how many elements in a array. I hope this will help u to understand . Little concept

提交回复
热议问题