How do I find the length of an array?

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

    You have a bunch of options to be used to get a C array size.

    int myArray[] = {0, 1, 2, 3, 4, 5, 7};

    1) sizeof() / sizeof():

    std::cout << "Size:" << sizeof(myArray) / sizeof(int) << std::endl;
    

    2) sizeof() / sizeof(*):

    std::cout << "Size:" << sizeof(myArray) / sizeof(*myArray) << std::endl;
    

    3) sizeof() / sizeof([]):

    std::cout << "Size:" << sizeof(myArray) / sizeof(myArray[0]) << std::endl;
    

提交回复
热议问题