How do I find the length of an array?

前端 未结 27 2937
轮回少年
轮回少年 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:11

    Lets say you have an global array declared at the top of the page

    int global[] = { 1, 2, 3, 4 };
    

    To find out how many elements are there (in c++) in the array type the following code:

    sizeof(global) / 4;
    

    The sizeof(NAME_OF_ARRAY) / 4 will give you back the number of elements for the given array name.

提交回复
热议问题