1

For a programming class, we have to make a procedure that only take in as parameter the pointer for an array: void myprocedure(int * myArray) and then outputs the elements in it. Since I only know how to do so using a for loop,I tried first to calculate the length of the array by using the line sizeof(myArray)/sizeof(int), only to later realize that sizeof(myArray) doesn't give the size of said array, but rather the size of the pointer.

I need a code that would either let me calculate the size of the array from my procedure, or output the elements in the array without having to know how much are there in.

  • 3
    Unless there's a specific value in the array to mark the end (like the null terminator in C-strings) there's no way to do it with only a pointer and non-template functions. – Some programmer dude Oct 28 '17 at 11:08
  • Every *array type* can quickly decompose into a pointer to the first element in the array. The regular way to pass it into a function is a pointer to the first element and a size. You should use `std::vector` or `std::array` – WhiZTiM Oct 28 '17 at 11:08
  • The function needs more parameters: array capacity and quantity of elements in the array. Arrays don't carry that information in their data structure. – Thomas Matthews Oct 28 '17 at 14:12

0 Answers0