C ++: two-dimensional array: is one dimension fixed?

I need to pass a method to a double [][6]method. But I do not know how to create this two-dimensional array.

6- a fixed size (or "literal constant" if my terminology is correct) that the method accepts. I tried something like this, but to no avail ...

double *data[6] = new double[6][myVariableSize];

So the method really looks like this:

int myMethod(double obj_data[][6]);

Thanks in advance

+3
source share
5 answers

I cannot say from the question what dimension is, but it may be worth a try:

double (*data)[6] = new double[myVariableSize][6];
+3
source

In C ++ you can use std::array<std::vector<double>, 6>.

typedef std::array<std::vector<double>, 6> my_array_t;

int myMethod( const my_array_t& obj_data );

std::array, boost::array.

+3

:

int myMethod(double obj_data[][6])
...
int myVariableSize = 10;
double (*data)[6] = new double[myVariableSize][6];
myMethod(data);

, !

+3

( ):

int myMethod(double obj_data[6][]);

.. ++, undefined. :

int myMethod(double **obj_data, const int numOfColumns, int numOfRows)
{
    // Set the element in the last column / row to 5
    obj_data[numOfRows-1][numOfColumns-1] = 5;

    return 0;
}


int main(int argc, char* argv[])
{
    // Define array size
    int myNumOfRows = 5;
    const int numOfColumns = 6;

    // Allocate memory
    double** data = new double*[myNumOfRows];
    for (int i = 0; i < myNumOfRows; ++i)
    {
        data[i] = new double[numOfColumns];
    }

    // Do something with the array
    myMethod(data, numOfColumns, myNumOfRows);

    return 0;
}
+1

, . :

int myMethod(double** obj_data)
{
    return 5;
}

int _tmain(int argc, _TCHAR* argv[])
{
    const int myVariableSize = 3;
    double** b = new double*[3];
    for (int i = 0 ; i < 3;i++)
    {
        b[i] = new double[myVariableSize];
    }

    myMethod(data);

    return 0;
}

2D- Boost ( ).

0

Source: https://habr.com/ru/post/1758914/


All Articles