Size of an array C++
arrays, c++, size
Solution
When you write `size(a)` then you're passing a pointer and not an array. Since the size of a pointer and an `int` is 4 or 8 (depending on ABI), you get `sizeof(int *)/sizeof int` (4/4=1 for 32-bit machines and 8/4=2 for 64-bit ones) which is 1 or 2.
In C++ when pass an array as an argument to a function, actually you're passing a pointer to an array.
Problem
Possible Duplicate: Sizeof array passed as parameter I was wondering why the output of the following code is 1 and 9. Is that because of undeclared array in function size? How can I separate "size of array" to a function? ``` #include "stdafx.h" #include <iostream> using namespace std; int size(int a[]) { return sizeof a/sizeof a[0]; } int main() { int a[] = {5,2,4,7,1,8,9,10,6}; cout << size(a) << endl; cout << sizeof a/sizeof a[0] << endl; system("pause"); return 0; } ```