0

Here's a simple compile error, I'm allocating double arrays like so:

double mixmu[][1]  = {{1},{-1}};
double mixvar[][1] = {{1},{1}};
double coef[]      = {1,1};

can I not pass these as double** objects?

error: no matching function for call to ‘MixtureModel::MixtureModel(int, int, double [2], double [2][1], double [2][1], Distribution*)’
./problems/MixtureModel.h:25: note: candidates are: MixtureModel::MixtureModel(int, int, double*, double**, double**, Distribution*)
fairidox
  • 3,358
  • 2
  • 28
  • 29
  • You'll have a much better time with vectors, but do yourself a favour and read up on how this works too. – chris May 31 '12 at 04:29
  • 11
    An array is not a pointer. *An array is not a pointer*. **An array is NOT a pointer**. – Adam Rosenfield May 31 '12 at 04:30
  • You can use an explicit cast to double**, but I am not sure whether this is a good idea. – nhahtdh May 31 '12 at 04:30
  • 5
    @Adam Rosenfield [But I heard that char `a[]` was identical to `char *a`](http://c-faq.com/aryptr/aryptr2.html) – cnicutar May 31 '12 at 04:31
  • 3
    For sake of reference/clarity the question should be updated with *how* the variables are used in said invocation. –  May 31 '12 at 04:34
  • 1
    Try reading [my answer](http://stackoverflow.com/a/8514058/541686)? – user541686 May 31 '12 at 04:35
  • 2
    @Mehrdad Yep, that answer makes it obvious, thanks for the comments everyone. – fairidox May 31 '12 at 04:41
  • possible duplicate of [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – fredoverflow May 31 '12 at 06:17
  • possible duplicate of [What is difference between types int** and int\[\]\[\]?](http://stackoverflow.com/questions/8395255/what-is-difference-between-types-int-and-int) – Bo Persson May 31 '12 at 07:12

1 Answers1

2

Do I need to dynamically allocate double arrays to pass them?

No you don't!
Your misconception/doubt stems from the (Incorrect)fact that, Arrays are pointers

No! Arrays are not pointers!!
An array name decays sometime to an pointer to its first element in scenarios where array name is not valid.

A two dimensional array does not decay to an double pointer. It decays to an pointer to array.

Your declaration needs to be:

MixtureModel::MixtureModel(int, int, double [2], double [2][1], double [2][1], Distribution*);

or

MixtureModel::MixtureModel(int, int, double *, double(*)[1], double (*)[1], Distribution*);

Good Read:
How do I use arrays in C++?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533