Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is, that a 2D array in the form <code>int**</code> requires an array of <code>int*</code> for the two step dereferencing, which simply does not exist when you declare an array with <code>int A[sz][sz];</code>.</p> <p>You can build it yourself like this:</p> <pre><code>int* pointers[sz]; for(size_t i = sz; i--; ) pointers[i] = A[i]; </code></pre> <p>This might seem absurd, but is rooted in the way C handles arrays: <code>A[i]</code> is of type <code>int ()[sz]</code>, which is the subarray of row <code>i</code>. But when you use that array in the assignment, it <em>decays</em> to a pointer to the first element in that subarray, which is of type <code>int*</code>. After the loop, <code>A</code> and <code>pointers</code> are two very different things (the type of <code>A</code> is <code>int ()[sz][sz]</code>)</p> <p>Sidenote: You say that you want to return this from a function. If your array is allocated on the stack, you <strong>must not return a pointer to its data</strong>, it will disappear the moment your function returns. You can only return pointers/references to objects that have either <code>static</code> storage or are part of another existing object. If you fail to comply with this, you are likely to get stack corruption.</p> <p><strong>Edit:</strong></p> <p>A little known fact about C is, that you can actually pass around pointers to real C arrays, not just the pointer types that an array decays to. Here is a small program to demonstrate this:</p> <pre><code>#include &lt;stddef.h&gt; #include &lt;stdio.h&gt; int (*foo(int size, int (*bar)[size][size], int y))[] { return &amp;(*bar)[y]; } int main() { int mySize = 30; int baz[mySize][mySize]; int (*result)[mySize]; result = foo(mySize, &amp;baz, 15); printf("%ld\n", (size_t)result - (size_t)baz); } </code></pre> <p>The expected output of this example program is 1800. The important thing is that the actual size of the array must be known, either by being a compile time constant, or by being passed along with the array pointer (and if it's passed along with the array pointer, the size argument must appear before the array pointer does).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload