开发者

Is it possible to reinterpret pointer as dimensioned array reference?

Suppose I have some pointer, which I want to reinterpret as static dimension array reference:

开发者_如何学运维double *p;
double (&r)[4] = ?(p); // some construct?

// clarify
template< size_t N> void function(double (&a)[N]);
...
 double *p;
function(p); // this will not work.
//  I would like to cast p as to make it appear as  double[N]

Is it possible to do so? how do I do it?


It's ugly:

double arr[4];
double* d = arr;

double (&a)[4] = *static_cast<double(*)[4]>(static_cast<void*>(d));

Be sure the array type matches what the pointer originally came from.


double *array;
...
...
int sizearray = sizeof(array)/sizeof(double);


Yes, it's called a vector :)

std::vector<double> myVariableArray(4)

EDIT: Rereading, it looks like you want to get the size an array was declared with. You can't do that -- that's a template method feature you can use on occasion. Since a double * doesn't even need to point to doubles there's little way a compiler could give you a sensible answer in any case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜