以数组名作函数参数时,实参数组与形参数组都不必定义长度,因此实参与形参的结合方式是地址结合,与数组长度无关。请问这句话的说法是正确的吗?
B. void fun( int *p[ ] ) {}
报错和A完全相同:形参会把[]退化成指针,这相当于定义了形参为二级指针;
当使用指针的时候,需要用数组指针
// testcpp.cpp : Defines the entry point for the console application.
//
#include "iostream"
using namespace std;
int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
void A1(int a[][3])
{
a[0][0] = -1;
}
void A2(int(* a)[3])
{
a[0][0] = -2;
}
int main()
{
A1(a);
cout << a[0][0] << endl; // -1
int (*p)[3] = a;
A2(p);
cout << a[0][0] << endl; // -2
cout<<(*p)<<endl; // 0x4a2020
cout<<(*p)[0]<<endl; // -2
cout<<*(p+1)<<endl; // 0x4a202c
cout<<*(p+1)[0]<<endl; // 4
cout<<*(*(p+1)+2)<<endl; // 6
return 0;
}