In C, when the array name is passed as a parameter to a function, it is degraded to a pointer, and sizeof should result in a pointer operation of 4. Examples are as follows:
#include <iostream>using namespacestd;voidSwap_arr (Char*Chararr) { intLen =sizeof(Chararr); cout<<"len ="<< Len <<Endl; Output Len =4}intMain () {CharChararr[] ="ABCDEFGH"; Swap_arr (Chararr); return 1;}
So if we need the size of the array in the function, we need one parameter to pass the array name, and the other to pass the array size. As shown below:
#include <iostream>using namespacestd;voidTest_arr (Char* Chararr,intLen) {cout<<"len ="<< Len <<Endl; Output: Len =8}intMain () {CharChararr[] ="ABCDEFGH"; intLen =sizeof(Chararr)/sizeof(chararr[0]) -1; Test_arr (Chararr,len); return 1;}
The "C language" array name is passed to the function, the reason that the array of sizeof becomes 4