0

I'm trying to make a function that returns the length of an array, but the function always returns 8

#include <iostream>
using namespace std;

int size(int arr[])
{
    return sizeof(arr);
}

int main()
{
    int v[] = {1, 2, 4};
    cout << sizeof(v) << endl;
    cout << size(v);

    return 0;
}
Ste.02
  • 13
  • 2
  • 1
    The parameter `arr` in your `size()` function is a pointer, not the array. It is 8 bytes on a 64bit system. Want to pass an array by value? Use `std::array`. – flowit Apr 04 '21 at 14:21

1 Answers1

1

This is because ‘sizeof’ on array function parameter ‘arr’ will return size of ‘int*’..

Rohith V
  • 1,089
  • 1
  • 9
  • 23