0

I want to calculate the length of an array inside a function, to use it for iteration. But the normal pointer method to calculate the array isnt working. Why? What changes can i make to my code to make it work? Im aware there are much better ways to go about getting the length, or to print the array, but what is fundamentally wrong in this method?

#include <iostream>
using namespace std;

void print(int ar[]){
    int len = *(&ar + 1) - ar;
    for(int i = 0; i < len; i++){
        cout << ar[i] << ' ';
    }
    cout << endl;
}
int main() {
    int aa[] = {2,3,1};
    print(aa);
}

Output of the above code is a blank line (only endl is being print)

When I write cout << len; inside the function, the output turns out to be -4292483, which seems like a random number.

Mamba
  • 43
  • 6
  • 5
    In a function parameter list, arrays are automatically replaced with pointers. `int arr[]` there means `int *arr`. – HolyBlackCat Jan 29 '22 at 16:13
  • @HolyBlackCat oh okay, but then how do i use that to calculate the length? and the pointer is a pointer to the array or the first element of the array? – Mamba Jan 29 '22 at 16:15
  • Why not just use `std::array`? – Jesper Juhl Jan 29 '22 at 16:16
  • In the so called C-style arrays, the pointer to the array is the pointer to the first element of that array. – navyblue Jan 29 '22 at 16:17
  • 2
    The info about the array size is irrevokably gone once the the array decays. You could of course make the function a pointer receiving a fixed-sized array reference: `template void print(int (&ar)[N]) { for (size_t i = 0; i != N; ++i) ... }` – fabian Jan 29 '22 at 16:23
  • The best change to your code is to use std::array<> or std::vector<> and avoid c-style arrays. If you are forced to use them you either have to use a template like the above comment from @fabian or pass the size as a parameter – drescherjm Jan 29 '22 at 16:28
  • *"then how do i use that to calculate the length?"* You pass it as a separate parameter. *"the pointer is a pointer to the array or the first element of the array"* The two have the same numerical value, but different types. Pointer to first element is `int *` (this is what you have), and pointer to whole array is `int (*)[N]`. – HolyBlackCat Jan 29 '22 at 16:41
  • @fabian ahh okay okay, thank you, size simply cant be gotten. Ill check out to use templates – Mamba Jan 29 '22 at 16:43
  • @HolyBlackCat wait, isnt a pointer to the whole array `int **` (thats the error im getting for some random variations in my code); what exactly is `int(*) [N]` ? – Mamba Jan 29 '22 at 16:45
  • 1
    `int **` is a pointer to pointer, not pointer to array. `int (*)[N]` is a "pointer to array of size N of int". A variable of this type would be declared like this: `int (*ptr)[N] = ...;`. – HolyBlackCat Jan 29 '22 at 16:48
  • @HolyBlackCat okay, now after i change my parameters of the `print` function as `void print(int (*ar)[]){ ... }`, i cant use the pointer to the array because size is unspecified, is it possible to use it as such or in another way without specifying the size? – Mamba Jan 29 '22 at 16:55
  • No, you have to pass the size separately, or make the function a template as suggested above. – HolyBlackCat Jan 29 '22 at 17:25

0 Answers0