[英]Is there a way of casting a pointer to an array type?
C arrays are somewhat difficult to understand syntactically in C++ and can take some getting used to. 在C ++中,C数组在语法上有点难以理解,并且可能需要一些时间来习惯。 Although a 1D array decays to a pointer: 虽然1D数组衰减到指针:
void fn1(int x[2]) {}
void fn2(int*x) {}
fn1()
and fn2()
have the same function signature. fn1()
和fn2()
具有相同的函数签名。
An array actually does have a type that includes how many elements are in the array. 数组实际上有一个类型,包括数组中有多少个元素。 As in: 如:
void fn(int (&)[2]) {}
fn()
will only accept a 2 element int array. fn()
只接受2个元素的int数组。
Thing is, I can only see that the array of a fixed number of elements can only be generated by stack, file scope or struct/class allocation with that signature: 事实是,我只能看到固定数量的元素的数组只能由具有该签名的堆栈,文件范围或结构/类分配生成:
int twoElementArray[2];
If I were to dynamically allocate it on the heap, I can't seem to get the same signature. 如果我要在堆上动态分配它,我似乎无法获得相同的签名。 I thought that I might be able to cast it, but without success: 我以为我可能会施展它,但没有成功:
int (&array)[2] = reinterpret_cast<int(&)[2]>(new int[2]); // FAIL!
Any ideas as to how this might be accomplished if at all? 有关如何实现这一点的任何想法吗?
EDIT: Although I selected an answer, it actually doesn't actually cast anything, but uses a definitely better method then casting (better not to cast if not required IMO). 编辑:虽然我选择了一个答案,但它实际上并没有投出任何东西,但是使用了一种绝对更好的方法然后进行投射(如果不是必需的IMO,最好不要施放)。 However, it technically doesn't answer the question since the question asked if there's "a way of casting a pointer to an array type?" 但是,它在技术上没有回答这个问题,因为问题是否有“一种指向数组类型的指针?” The answer is yes. 答案是肯定的。
int (&array)[2] = *reinterpret_cast<int(*)[2]>(new int[2]); // SUCCESS!
Note that I don't necessarily recommend doing this, but it does answer the question. 请注意,我不一定建议这样做,但它确实回答了这个问题。 If I needed to convert a pointer to an array type though, that would be how to do it. 如果我需要将指针转换为数组类型,那将是如何做到的。 Read the picked answer for a better solution when using operator new[]
. 使用operator new[]
时,请阅读所选答案以获得更好的解决方案。
If I understand your problem correctly, you'd want to do something like this: 如果我理解你的问题,你会想做这样的事情:
// allocate an array of one int[2] dynamically
// and store a pointer to it
int(*p)[2] = new int[1][2];
// now initialize a reference to it
int(&array)[2] = *p;
// delete the array once you no longer need it
delete[] p;
I think this is what you are looking for. 我想这就是你要找的东西。 For the heap, a two-dimensional array int[M][N]
decays to int(*)[N]
. 对于堆,二维数组int[M][N]
衰减到int(*)[N]
。 To pass it by reference, dereference it (see m
below): 要通过引用传递它,请取消引用它(参见下面的m
):
#include <iostream>
using namespace std;
void func(int (&x)[2])
{
cout << x[0] << ' ' << x[1] << endl;
}
int main()
{
// on the heap
auto m = new int[1][2];
m[0][0] = 1; m[0][1] = 2;
auto n = new int[1][3];
n[0][0] = 4; n[0][1] = 5; n[0][2] = 6;
// on the stack
int o[2] = {7,8};
int p[3] = {9,10};
func(*m);
//func(*n); // doesn't compile, wrong size
func(o);
//func(p); // doesn't compile, wrong size
}
Output: 输出:
1 2
7 8
Based on Mark answer, this could be better: 基于马克答案,这可能会更好:
template <typename T>
void func(const T &x)
{
cout << x[0] << ' ' << x[1] << endl;
}
the only bad think is that this code is valid: 唯一不好的想法是这段代码是有效的:
cout << x[3] << endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.