Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a float array in C++

I currently have a 4x4 matrix class in C++ and I store each value as a float:

Matrix4d::Matrix4d(const float& m00, const float& m01, const float& m02, const float& m03,
                   const float& m10, const float& m11, const float& m12, const float& m13,
                   const float& m20, const float& m21, const float& m22, const float& m23,
                   const float& m30, const float& m31, const float& m32, const float& m33)
{
    _m00 = m00;
    _m01 = m01;
    _m02 = m02;
    _m03 = m03;
    _m10 = m10;
    _m11 = m11;
    _m12 = m12;
    _m13 = m13;
    _m20 = m20;
    _m21 = m21;
    _m22 = m22;
    _m23 = m23;
    _m30 = m30;
    _m31 = m31;
    _m32 = m32;
    _m33 = m33;
}

My question is, how can I return a float array of this data? I have no problem creating the array in the class for example:

float arrayToReturn[16] = { m00, m01, m02, m03, ... m33 };

However I can not return this value from the class. I've read about returning a pointer to the array, but have had no luck with it.

like image 919
ingh.am Avatar asked Feb 25 '10 13:02

ingh.am


People also ask

Can I return an array in C?

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

Can we return float value in C?

Yes, the result of mid() may be converted to double , passed to printf() , which then converts to float for printing purposes.

Can we declare a float array in C?

You can define the array without the size. but it should be in this way: float array[] = {3.544, 5.544, 6.544, 6.544};

What is float array in C?

A float is a 4-byte floating point value. A char is a 1-byte single character (like "a" or "3"). A string is declared as an array of characters. There are a number of derivative types: double (8-byte floating point value)


1 Answers

  1. Don't pass floats by const reference, pass them by value.

  2. I assume you want to return the array so you can do indexing? Then don't return an array from you matrix class. Instead, overload the [] operator or something.

  3. Also, I wouldn't use 16 member variables but one array instead. Makes indexing a lot easier.

Here is how I would probably do it:

class Matrix4d
{
    float matrix[4][4];

public:

    Matrix4d(float m00, float m01, float m02, float m03,
             float m10, float m11, float m12, float m13,
             float m20, float m21, float m22, float m23,
             float m30, float m31, float m32, float m33)
    {
        matrix[0][0] = m00;
        matrix[0][1] = m01;
        matrix[0][2] = m02;
        matrix[0][3] = m03;
        matrix[1][0] = m10;
        matrix[1][1] = m11;
        matrix[1][2] = m12;
        matrix[1][3] = m13;
        matrix[2][0] = m20;
        matrix[2][1] = m21;
        matrix[2][2] = m22;
        matrix[2][3] = m23;
        matrix[3][0] = m30;
        matrix[3][1] = m31;
        matrix[3][2] = m32;
        matrix[3][3] = m33;
    }

    float* operator[](int i)
    {
        return matrix[i];
    }

    const float* operator[](int i) const
    {
        return matrix[i];
    }
};

int main()
{
    Matrix4d test(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 16);
    test[3][2] = 15;
}
like image 138
fredoverflow Avatar answered Sep 24 '22 10:09

fredoverflow