0

How do I get the size of bits of an Array from a function

int NumberOfElements(int Array[]);

int main()
{
    int Array[] = { 5,5,6,5,5 };
    std::cout << NumberOfElements(Array);
}
int NumberOfElements(int Array[]) {
    return sizeof(Array);
}

It's returning 4. Result should be 20.

Random Guy
  • 61
  • 7

1 Answers1

5

Arrays decay into pointers when passed as arguments to functions etc. The size 4 means that the pointer has that size. It does not tell you anything about the number of elements in the actual array.

You may want to use a std::vector<int> instead where the size is part of its interface:

#include <vector>

int main()
{
    std::vector<int> Array{ 5,5,6,5,5 };
    std::cout << NumberOfElements(Array);
}
int NumberOfElements(const std::vector<int>& Array) {
    return Array.size();
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108