0

I need to calculate the size from the function but it keeps said error dividing sizeof a pointer by another value

int Get_size(int a[])
{
    return sizeof(a) / sizeof(a[0]);
}
int main()
{
    int arr[] = { 1,3,5,6 };
    std::cout << Get_size(arr);
}```
  • 2
    `template int Get_size(int (&a)[N]) { return N; }` – rafix07 Apr 23 '21 at 04:43
  • There are various posts out here along the lines of "sizeof array passed to function" explaining what the issue is. For instance, https://stackoverflow.com/questions/8269048/length-of-array-in-function-argument – TheUndeadFish Apr 23 '21 at 04:54
  • See the section on "pitfalls" in the duplicate Q&A. Bookmark this and refer to it any time you are confused about arrays. Look for _"5.3 Pitfall: Using the C idiom to get number of elements."_ [here](https://stackoverflow.com/a/7439261/1553090) – paddy Apr 23 '21 at 05:18

0 Answers0