In this code snippet i passed array A in sumOfElements(A) function as it hold the reference of the first value of array A which is an integer, it's size should be 4. Instead it returning 8.
#include <iostream>
using namespace std;
int sumOfElements(int A[])
{
int i, sum = 0;
int size = (sizeof(A) / sizeof(A[0]));
cout << "Size of A " << sizeof(A) << endl;
cout << "Size of A[0] " << sizeof(A[0]) << endl;
for (i = 0; i < size; i++)
{
sum += A[i];
}
return sum;
}
int main()
{
// Arrays as function arguments
int A[] = {1, 2, 3, 4, 5};
int total = sumOfElements(A);
// cout << "Sum of elements = " << total << endl;
cout << "Size of A " << sizeof(A) << endl;
cout << "Size of A[0] " << sizeof(A[0]) << endl;
return 0;
}
Result
Size of A 8
Size of A[0] 4
Size of A 20
Size of A[0] 4
Expected result is
Size of A 4
Size of A[0] 4
Size of A 20
Size of A[0] 4