我正在研究多维数组。矩阵的数据和尺寸在运行时给出,并且我尝试使用如下类型的别名指针访问数据。
它与我的代码配合得很好,但是我不确定它是否在c ++标准之内。
我可以使用这样的类型别名:未知维数组指针吗?
cin >> a; //1
cin >> b; //2
cin >> c; //3
int* buf = new int[100]; //just to allocate some memory for test
using arr_t = int (*)[a][b][c]; //type alias of pointer of array
arr_t arr = reinterpret_cast<arr_t>(buf);
buf[3] = 1;
cout << arr[0][1][0] << endl; // 1 (It's easier than buf[b*c*0 + c*1 + 0])
cout << (void*)&buf[3] << endl; //0x18b8e78
cout << (void*)&(*arr)[0][1][0] << endl; //0x18b8e78
具有运行时值的[int (*)[a][b][c]
使用VLA(可变长度数组)扩展,因此不是标准C ++。
即使使用该扩展名,演员也违反了严格的别名规则。