i have this code looks like the sizeof(skyline) returns always 8 and when deviding it with sizeof(int) that is 4 give me wrong array size . The code :
int calcualteSkyLine(const int* skyline) { int arrSize = sizeof(skyline) / sizeof(int); for(int i=0;i <=arrSize;i++) { std::cout << skyline[i] << std::endl; } } int main(int argv,char** argc) { const int buldingArry[] = {1,3,2,1,2,1,5,3,3,4,2}; calcualteSkyLine(buldingArry); return 0; } arrSize is always 8 .. therefor the array cant be printed , what is wrong here ? if i replace the "arrSize" with 10 all works
std::sizeto get the size of a c-array. WHen used wrong it fails to compile rather than returning bogus results likesizeofdoesstd::array(non-resizable array) orstd::vector(resizable array) in C++ if you want to pass the length of the array too. E.g.int calculateSkyLine(const std::vector<int>& skyline);And use [range based for loop] to loop over all items in the array (en.cppreference.com/w/cpp/language/range-for) (and don't have to manually worry about the size at all) .