This question has already been answered:

Dear Colleagues!

The compiler does not understand me. It is well known that the dimension of the array is int size = (sizeof(b) / sizeof(*b)); Where b is some array. So in the example below, I get the dimension of the array by putting this command into a separate function and executing this command in main :

 #include "stdafx.h" #include <windows.h> #include <iostream> using namespace std; //Функция которая считает и печатает размерность массива. void ArrLength(int a[]) { int j = (sizeof(a) / sizeof(*a));; //определяем размерность массива cout << "Размерность массива в функции ArrLength = " << j << endl; } int main() { setlocale(LC_ALL, "Russian"); int b[] = {3, 5, 8, 3, 0, 32, 18, 65}; //объявление некоторого массива int size = (sizeof(b) / sizeof(*b)); //выяснение размерности некоторого массива cout << "Размерность массива size = " << size << endl; ArrLength(b); system("PAUSE"); return 0; } 

To my deepest surprise, prints:

 Размерность массива size = 8 Размерность массива в функции ArrLength = 2 

That is, it gives two different answers. And if I am ready to agree with the first, then the second seems to me obviously erroneous.

Question: what can explain the observed behavior?

Reported as a duplicate by participants αλεχολυτ , Harry c ++ Jan 7 '17 at 10:57 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    2 answers 2

    Function parameters declared as arrays are implicitly ordered into pointers to array element types.

    Therefore, the following function declarations

     void ArrLength(int a[]); void ArrLength(int a[100]); void ArrLength(int *a); 

    Equivalent and declare one and the same function, the parameter of which will be an int * pointer.

    You can include all these declarations in the program at the same time, since the same function can be declared (unlike the function definition) several times.

    Therefore, inside this function

     void ArrLength(int a[]) { int j = (sizeof(a) / sizeof(*a));; //определяем размерность массива cout << "Размерность массива в функции ArrLength = " << j << endl; } 

    the function parameter a is of type int * and the expression

     sizeof(a) / sizeof(*a) 

    equivalent to the expression

     sizeof( int * ) / sizeof( int ) 

    and depending on the platform will be either 2 or 1.

    If you want to write yourself a function that will return the number of elements in the array, then you should write a template function in which the array is passed by reference.

    for example

     template <typename T, size_t N> size_t ArrLength( const T (&a)[N] ) { return N; } 

    In C ++, you can use the std::extent structure to get the number of elements in an array. For example,

     #include <iostream> #include <type_traits> int main() { int a[4][6]; std::cout << std::extent<decltype(a)>::value << std::endl; std::cout << std::extent<decltype(a), 1>::value << std::endl; } 

    Output of the program to the console

     4 6 
    • To go nuts. The question was asked 41 minutes ago. The answer was given 32 minutes ago. So you painted everything in detail in 9 minutes? - Andrew Kachalin
    • four
      @AndrewKachalin This is one of the frequently asked questions about C / C ++, so the answer is already written, as they say, on the machine. No need to go into the standard language to find out any details. - Vlad from Moscow

    The fact that, when passed to a function, the array is implicitly reduced to a pointer. So you try to figure out sizeof(int*)/sizeof(int) .

    By the way, it is immediately obvious that you are creating a 64-bit application :)