-2

I was just messing around playing with some code then i suddenly see that after passing a array by reference sizeof(arr)/sizeof(arr[0]) is giving me other output than i expected.If i am just passing the reference then the len=1 may be justified since sizeof(arr[0])/sizeof(arr[0]).What is wrong with this any concept i am missing?

#include<iostream>
using namespace std;

void display(int *arr)
{
    int len=sizeof(arr)/sizeof(arr[0]);
    cout<<len<<"\n";
    for(int i=0;i<50;++i)
    {
        cout<<arr[i];
    }
    cout<<"\n";
}

int main()
{
    int arr[50]={0};
    for(int i=0;i<50;++i)
    {
        arr[i]=i;
    }
    display(arr);
    return 0;
}

Why is output of lenth not 50 as it should be?Why is it only 2 instead of 50.

Fareanor
  • 5,900
  • 2
  • 11
  • 37
rootOcean
  • 13
  • 2
  • 3
    `arr` is a pointer `sizeof` of a pointer is 4 o 8 bytes depending on compile target. – Federico Oct 28 '19 at 09:54
  • 1
    please indent your code – Marco Bonelli Oct 28 '19 at 09:55
  • 2
    First of all, `sizeof` is an *operator* and not a function. And secondly, the size of a pointer is the size of the pointer itself and not what it might point to. – Some programmer dude Oct 28 '19 at 09:55
  • 3
    To solve your problem you have three possible solutions: Either pass the size along as an argument to the function; Or use templates to deduce the size passing a reference to the array; Or use `std::vector` or `std::array` instead. – Some programmer dude Oct 28 '19 at 09:56
  • 1
    `sizeof` is an operator, not a function. And the behaviour you see is correct. In `display()`, the argument `arr` is a pointer, and `sizeof(arr)` computes the size of the pointer, not the array it points at. The `sizeof(arr)/sizeof(arr[0])` technique only works if `arr` is an array. Passing an array to a function, converts the array to a pointer, so the function receives a pointer not an array. – Peter Oct 28 '19 at 10:01

1 Answers1

0

Whenever you try to pass an array to any function. The array is implicitly converted to pointer to first element of array. Here in above code as you passed integer array it get converted to integer pointer which is pointing an array. That's why you are getting such result. In array cases you need to pass size explicitly in the function. So sizeof is working fine , it is dividing size of "pointer to array" with the "size of one element".

Reason behind such casting is that "As you must know that arguments are copied into parameters of the function. But in case of arrays.Array can be of variable size they may be larger size. So it's unnecessary to copy such large array.That's why language implement this casting automatically. "

Nikhil Badyal
  • 1,589
  • 1
  • 9
  • 20
  • I've reworded your answer a little bit (I would guess English is not your native language). If you don't like the change, feel free to revert it. – Martin Bonner supports Monica Oct 28 '19 at 10:18
  • Note that `arr` is a `int*` (pointer to integer). It points to the first element of an array. It's not the same as a pointer to an array (which is pretty much never used). It is fair to say it points to an array, but it's type is not "pointer to array". A pointer to array would not have the problem described here : https://godbolt.org/z/QytHlH – François Andrieux Oct 28 '19 at 13:56