为什么两种情况下数组的大小都不相同

Why is the size of array not same in both the cases?

本文关键字:两种 情况下 数组 为什么      更新时间:2023-10-16
#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); 
}

输出:

24
8

为什么即使我打印相同数组的大小,我的输出在这两种情况下也不相同?

在第一次调用中,它显示数组的大小。但在第二种情况下,它显示传递的指针的大小。

当数组通过函数传递时,它们会衰减为指针。所以通过后,:

sizeof(array)

显示指针的大小。