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

前端 未结 2 502
不思量自难忘°
不思量自难忘° 2021-01-03 04:20

I have a problem when I try to pass the pointer of an array (which contains parameters needed by some functions in my program) to a structure, which then should be passed to

2条回答
  •  失恋的感觉
    2021-01-03 05:23

    double d[4][3]; is an array of arrays. double b**; is a pointer to pointer. These types are not the same (the common "arrays are just pointers" you might have read on the internet is wrong).

    Elements of d are of type double[3]. Arrays, when passed around, decay to pointers to their first element (see section 4.2. of C++ standard). d will decay to double(*)[3] (a pointer to array of 3 doubles).

    Long story short, double(*)[3] is not convertible to double** and this is what compiler is telling you.

    If you need to keep d as it is, you need to declare b as double (*b)[3];

    For more in-depth explanation, refer to this SO question.

提交回复
热议问题