一种传已知数组长度的数组指针的方法

#include <iostream>

using namespace std;

void f(int (&a)[3] )   // why?
{
    cout << sizeof(a)/sizeof(int) << endl;

    for(auto i:a)
    {
        cout << i << endl;
    }
    
    for(int i = 0; i < 3; ++i)
    {
        cout << a[i] << endl;
    }
}

int main()
{
    int a[3]{1,2,3};
    f((&a)[0] );
    return 0;
}

 

posted @ 2020-09-04 09:36  黎曼猜想  阅读(139)  评论(0)    收藏  举报