0

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

8
  • 1
    you pass a pointer to the function. Pointers are not arrays. Commented Jan 28, 2023 at 13:09
  • Does this answer your question? stackoverflow.com/questions/5724171/… Commented Jan 28, 2023 at 13:09
  • you should use std::size to get the size of a c-array. WHen used wrong it fails to compile rather than returning bogus results like sizeof does Commented Jan 28, 2023 at 13:10
  • You don't pass an array, you pass a pointer. Use std::array (non-resizable array) or std::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) . Commented Jan 28, 2023 at 13:12
  • It looks like you are learning C++ from an outdated source, look at cppreference for examples. Get a recent C++ book or have a go at learncpp.com (that's pretty decent, and pretty up-to-date). Commented Jan 28, 2023 at 13:15

1 Answer 1

1

What your code would look like in current C++ (and a little extra, since I needed a return value ;)

#include <numeric> #include <iostream> #include <vector> int calcualteSkyLine(const std::vector<int>& skyline) { for(const int value : skyline) { std::cout << value << "\n"; } // return the sum of all values (another nice thing you can do with numeric/vector) // is to avoid (visible) for loops completely return std::accumulate(skyline.begin(), skyline.end(), 0); } int main(int argv, char** argc) { std::vector<int> buldingArray{ 1,3,2,1,2,1,5,3,3,4,2 }; auto sum = calcualteSkyLine(buldingArray); std::cout << "sum = " << sum; return 0; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks , but what if i needed to use int array not vector ?
A std::vector<int> is a C++ integer array :) Why would you need to use something else? If it is your teacher telling you to do so then he is teaching C++ as it was before 1998 (and I know I've been using C++ since 1995). If you'd like I'd be happy to talk to him/her. Because still using"C" style arrays is just asking for bugs
For reference : https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-newdelete. Which basically means, use datastructures from the standard library or hide new/delete inside datastructure of your own.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.