1
#include<iostream>
using namespace std;
void f(int arr[])
{
    int a=sizeof(arr);
    cout<<a;
}
int main()
{
    int n;
    cin>>n;
    int arr[n];
    int a=sizeof(arr);
    cout<<a<<"\n";
    f(arr); 
}

Output:

24
8

Why is my output not same in both the cases even when i am printing the size of same array?

Krease
  • 15,805
  • 8
  • 54
  • 86

1 Answers1

0

In the first call it shows the size of array. But in the second case it shows the size of the pointer passed.

When arrays are passed through functions they decay into pointers. So after passing, the:

sizeof(array)

Shows the size of the pointer.

Aniruddha Sarkar
  • 1,903
  • 2
  • 14
  • 15