如何投放简单的指针固定大小的多维数组? [英] How to cast simple pointer to a multidimensional-array of fixed size?

查看:173
本文介绍了如何投放简单的指针固定大小的多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它指向一个浮点阵列。根据其他的条件,我知道该指针实际指向的2x2或3x3矩阵。 (其实内存最初是这样分配的,例如浮动M [2] [2])最重要的是我想使这个决心在函数体,而不是函数的参数。

 无效calcMatrix(INT脸,浮动* matrixReturnAsArray)
{
    //在这里,我宁愿在自然矩阵符号工作
    如果(is2x2)
    {
        // ###铸造matrixReturnAsArray到somethingAsMatrix [2] [2]
        somethingAsMatrix [0] [1] = 2.002;
        //等。
    }
    否则,如果(is3x3)
    {//等等...
    }}

我知道我可以使用模板和其他技术更好地解决这个问题。我的问题实际上是关于如何使这样的投在###评论。工作在C ++中。


解决方案

 浮动(* somethingAsMatrix)[2] =(浮点(*)[2])matrixReturnAsArray;

I have a function that takes a pointer to a floating point array. Based on other conditions, I know that pointer is actually pointing to a 2x2 OR 3x3 matrix. (in fact the memory was initially allocated as such, e.g. float M[2][2] ) The important thing is I want to make this determination in the function body, not as the function argument.

void calcMatrix( int face, float * matrixReturnAsArray )
{
    // Here, I would much rather work in natural matrix notation
    if( is2x2 )
    {
        // ### cast matrixReturnAsArray to somethingAsMatrix[2][2]
        somethingAsMatrix[0][1] = 2.002;
        // etc..
    }
    else if(is3x3)
    { //etc...
    }

}

I am aware that I could use templates and other techniques to better address this problem. My question is really about how to make such a cast at the ### comment. Working in C++.

解决方案

float (*somethingAsMatrix)[2] = (float (*)[2]) matrixReturnAsArray;

这篇关于如何投放简单的指针固定大小的多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆