How to pass a pointer of a static 2D array to a structure / class?

I have a problem when I try to pass an array pointer (which contains the parameters needed by some functions in my program) to a structure, which then needs to be passed to these functions. GSL, for example, wants me to pass parameters this way.

An example of a small program is as follows:

#include <iostream>

using namespace std;

struct myparams
{
    double  * a;
    double ** b;
};

int main()
{
    double c[10]   = {0,1,2,3,4,5,6,7,8,9};
    double d[4][3] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};

    double** e = new double*[4];
    for (int i = 0; i < 4; i++) {
       e[i] = new double[3];
    }

    myparams params;

    // THIS WORKS:
    params.a = c;
    for (int i = 0; i < 10; i++) {
        cout << params.a[i] << endl;
    }

    // THIS DOESN'T WORK
    params.b = d;

    // THIS WORKS:
    params.b = e;

    delete[] e;
}

What is the problem with

 params.b = d

The compiler complains about "cannot convert 'double[4][3]' to 'double**' in assignment"or something like that (translation from German).

+5
source share
2 answers

double d[4][3];- an array of arrays. double b**;- pointer to pointer. These types do not match (the generic “arrays are just pointers” that you may have read on the Internet are wrong ).

d double[3]. , , (. 4.2 ++). d double(*)[3] ( 3 ).

, double(*)[3] double**, , .

d , b double (*b)[3];

. SO.

+7

, ( , ), . .

, , :

char aa[2][2] = { { 'a', 'b' }, { 'c', 'd' } };
char **pp;

pp = new char*[2];
pp[0] = new char[2];
pp[1] = new char[2];

aa :

+----------+----------+----------+----------+
| aa[0][0] | aa[0][1] | aa[1][0] | aa[1][1] |
+----------+----------+----------+----------+

"array" pp :

+------+------+
| a[0] | a[1] |
+------+------+
   |      |
   |      v
   |      +---------+---------+
   |      | a[0][0] | a[0][1] |
   |      +---------+---------+
   V
   +---------+---------+
   | a[0][0] | a[0][1] |
   +---------+---------+

"array" pp ""

+6

All Articles