How do I find the length of an array?

前端 未结 27 3462
轮回少年
轮回少年 2020-11-21 23:10

Is there a way to find how many values an array has? Detecting whether or not I\'ve reached the end of an array would also work.

27条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 23:19

    sizeof(array_name) gives the size of whole array and sizeof(int) gives the size of the data type of every array element.

    So dividing the size of the whole array by the size of a single element of the array gives the length of the array.

     int array_name[] = {1, 2, 3, 4, 5, 6};
     int length = sizeof(array_name)/sizeof(int);
    

提交回复
热议问题