<p>I have a class to parse a matrix that keeps the result in an array member:</p>

<pre><code>class Parser
{
  ...
  double matrix_[4][4];
};
</code></pre>

<p>The user of this class needs to call an API function (as in, a function I have no control over, so I can't just change its interface to make things work more easily) that looks like this:</p>

<pre><code>void api_func(const double matrix[4][4]);
</code></pre>

<p>The only way I have come up with for the caller to pass the array result to the function is by making the member public:</p>

<pre><code>void myfunc()
{
  Parser parser;
  ...
  api_func(parser.matrix_);
}
</code></pre>

<p>Is this the only way to do things? I'm astounded by how inflexible multidimensional arrays declared like this are. I thought <code>matrix_</code> would essentially be the same as a <code>double**</code> and I could cast (safely) between the two. As it turns out, I can't even find an <em>unsafe</em> way to cast between the things. Say I add an accessor to the <code>Parser</code> class:</p>

<pre><code>void* Parser::getMatrix()
{
  return (void*)matrix_;
}
</code></pre>

<p>This will compile, but I can't use it, because there doesn't seem to be a way to cast back to the weirdo array type:</p>

<pre><code>  // A smorgasbord of syntax errors...
  api_func((double[][])parser.getMatrix());
  api_func((double[4][4])parser.getMatrix());
  api_func((double**)parser.getMatrix()); // cast works but it's to the wrong type
</code></pre>

<p>The error is:</p>

<blockquote>
  <p>error C2440: 'type cast' : cannot convert from 'void *' to 'const double [4][4]'</p>
</blockquote>

<p>...with an intriguing addendum:</p>

<blockquote>
  <p>There are no conversions to array types, although there are conversions to references or pointers to arrays</p>
</blockquote>

<p>I can't determine how to cast to a reference or pointer to array either, albeit that it probably won't help me here.</p>

<p>To be sure, at this point the matter is purely academic, as the <code>void*</code> casts are hardly cleaner than a single class member left public!</p>

[{'answer_id': 55118, 'author': 'Serafina Brocious', 'author_id': 4977, 'author_profile': 'https://Stackoverflow.com/users/4977', 'pm_score': 2, 'selected': False, 'text': "<p>I've used a union like this to pass around matrices in the past:</p>\n\n<pre><code>union matrix {\n    double dflat[16];\n    double dmatr[4][4];\n};\n</code></pre>\n\n<p>Then pass a pointer in to your setter and copy the data into the matrix in your class.</p>\n\n<p>There are ways of handling this otherwise (that are more generic), but this solution tends to be the cleanest in the end, in my experience.</p>\n"}, {'answer_id': 55149, 'author': 'Shog9', 'author_id': 811, 'author_profile': 'https://Stackoverflow.com/users/811', 'pm_score': 5, 'selected': True, 'text': "<p>Here's a nice, clean way:</p>\n\n<pre><code>class Parser\n{\npublic:\n   typedef double matrix[4][4];\n\n   // ...\n\n   const matrix&amp; getMatrix() const\n   {\n      return matrix_;\n   }\n\n   // ...\n\nprivate:\n  matrix matrix_;\n};\n</code></pre>\n\n<p>Now you're working with a descriptive type name rather than an array, but since it's a <code>typedef</code> the compiler will still allow passing it to the unchangeable API function that takes the base type.</p>\n"}, {'answer_id': 55167, 'author': 'Andrew', 'author_id': 826, 'author_profile': 'https://Stackoverflow.com/users/826', 'pm_score': 3, 'selected': False, 'text': '<p>Try this. It compiles cleanly on gcc 4.1.3:</p>\n\n<pre><code>typedef double FourSquare[4][4];\n\nclass Parser\n{\n  private:\n    double matrix_[4][4];\n\n  public:\n    Parser()\n    {\n        for(int i=0; i&lt;4; i++)\n          for(int j=0; j&lt;4; j++)\n            matrix_[i][j] = i*j;\n    }\n\n  public:\n    const FourSquare&amp; GetMatrix()\n    {\n        return matrix_;\n    }\n};\n\nvoid api_func( const double matrix[4][4] )\n{\n}\n\nint main( int argc, char** argv )\n{\n    Parser parser;\n    api_func( parser.GetMatrix() );\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 55183, 'author': 'Pete Kirkham', 'author_id': 1527, 'author_profile': 'https://Stackoverflow.com/users/1527', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>I thought matrix_ would essentially be the same as a double**</p>\n</blockquote>\n\n<p>In C there are true multi-dimensional arrays, not arrays of pointers to arrays, so a double[4][4] is a contiguous array of four double[4] arrays, equivalent to a double[16], not a (double*)[4]. </p>\n\n<blockquote>\n  <p>There are no conversions to array types, although there are conversions to references or pointers to arrays\n  Casting a value to a double[4][4] would attempt to construct one on the stack  - equivalent to std::string(parser.getMatrix()) - except that the array doesn't supply a suitable constructor. You probably did't want to do that, even if you could.</p>\n</blockquote>\n\n<p>Since the type encodes the stride, you need a full type (double[][] won't do). You can reinterpret cast the void* to ((double[4][4])*), and then take the reference. But it's easiest to typedef the matrix and return a reference of the correct type in the first place:</p>\n\n<pre><code>typedef double matrix_t[4][4];\n\nclass Parser\n{\n    double matrix_[4][4];\npublic:\n    void* get_matrix () { return static_cast&lt;void*&gt;(matrix_); }\n\n    const matrix_t&amp; get_matrix_ref () const { return matrix_; }\n};\n\nint main ()\n{\n    Parser p;\n\n    matrix_t&amp; data1 = *reinterpret_cast&lt;matrix_t*&gt;(p.get_matrix());\n\n    const matrix_t&amp; data2 = p.get_matrix_ref();\n}\n</code></pre>\n"}, {'answer_id': 55341, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': '<p>To elaborate on the selected answer, observe this line</p>\n\n<pre><code>const matrix&amp; getMatrix() const\n</code></pre>\n\n<p>This is great, you don\'t have to worry about pointers and casting. You\'re returning a <em>reference</em> to the underlying matrix object. IMHO references are one of the best features of C++, which I miss when coding in straight C.</p>\n\n<p>If you\'re not familiar with the difference between references and pointers in C++, <a href="https://isocpp.org/wiki/faq/references" rel="nofollow noreferrer">read this</a></p>\n\n<p>At any rate, you do have to be aware that if the <code>Parser</code> object which actually owns the underlying matrix object goes out of scope, any code which tries to access the matrix via that reference will now be referencing an out-of-scope object, and you\'ll crash.</p>\n'}]
['https://Stackoverflow.com/questions/55093', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4790/']