Element count of an array in C++

前端 未结 11 1722
[愿得一人]
[愿得一人] 2020-12-05 10:38

Let\'s say I have an array arr. When would the following not give the number of elements of the array: sizeof(arr) / sizeof(arr[0])?

I can

11条回答
  •  死守一世寂寞
    2020-12-05 10:58

    _countof(my_array) in MSVC

    I can thing of only one case: the array contains elements that are of different derived types of the type of the array.

    Elements of an array in C++ are objects, not pointers, so you cannot have derived type object as an element.

    And like mentioned above, sizeof(my_array) (like _countof() as well) will work just in the scope of array definition.

提交回复
热议问题