Element count of an array in C++

前端 未结 11 1699
[愿得一人]
[愿得一人] 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:43

    I know is old topic but what about simple solution like while loop?

    int function count(array[]) {
    
        int i = 0;
    
        while(array[i] != NULL) {
    
            i++;
    
        }
    
        return i;
    
    }
    

    I know that is slower than sizeof() but this is another example of array count.

提交回复
热议问题