现在的位置: 首页 > 综合 > 正文

void f(int(&p)[3]){} 和void f(int(*p)[3]){}的区别

2018年03月22日 ⁄ 综合 ⁄ 共 472字 ⁄ 字号 评论关闭

#include<iostream>
using namespace std;

void f(int(&p)[3]){
    
    cout<<p[0]<<endl;
    cout<<p[2]<<endl;
}

int main(){
    int a1[3]={1,2,3};
    cout<<a1<<endl;
    cout<<&a1<<endl;
    f(a1);
}

编译后输出:

0xbfbb8eb4
0xbfbb8eb4
1
3

#include<iostream>
using namespace std;

void f(int(*p)[3]){
    
    cout<<p[0]<<endl;
    cout<<p[2]<<endl;
}

int main(){
    int a1[3]={1,2,3};
    cout<<a1<<endl;
    cout<<&a1<<endl;
    f(&a1);
}

编译后输出:

0xbff21e84
0xbff21e84
0xbff21e84
0xbff21e9c

由此比较可以看出,
void f(int(*p)[3]){}是个函数指针数组,指针数组属于二级指针

抱歉!评论已关闭.