Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Everyone else has already told you why you shouldn't do this as it is written, but here are the types you are interested in.</p> <p>Given a declaration <code>int a[3]</code>, the type of the expression <code>&amp;a</code> is <code>int (*)[3]</code> (<em>not</em> <code>int **</code>), or "pointer to 3-element array of int", such as</p> <pre><code>void f() { int a[3] = {1,2,3}; int (*aptr)[3] = &amp;a; ... } </code></pre> <p>and the signature for a function returning that type would be <code>int (*fun())[3] {...}</code>. </p> <p>One other option nos didn't show is this:</p> <pre><code>int (*fun())[3] { int (*aptr)[3] = malloc(sizeof *aptr); if (aptr) { (*aptr)[0] = 1; // array pointer must be deferenced before applying (*aptr)[1] = 2; // the subscript. (*aptr)[2] = 3; } return aptr; } </code></pre> <p>although this isn't terribly useful; you don't normally see allocations of single, fixed-size arrays like this. Somewhat more useful is allocating an array of those arrays:</p> <pre><code>int (*fun(size_t count))[3] { int (*aptr)[3] = malloc(sizeof *aptr * count); if (aptr) { size_t i; for (i = 0; i &lt; count; i++) { aptr[i][0] = 1; // aptr[i] implicitly dereferences aptr, so aptr[i][1] = 2; // there's no need for an explicit dereference aptr[i][2] = 3; // here. } } return aptr; } </code></pre> <p>Even so, if somebody needs to allocate a fixed-size array type, they usually hide it behind a typedef:</p> <pre><code>typedef int fixedSizeRecord[SOME_SIZE]; ... fixedSizeRecord *fun(size_t count) { fixedSizeRecord *aptr = malloc(sizeof *aptr * count); if (aptr) { // initialize contents as necessary for (size_t i = 0; i &lt; count; i++) for (j = 0; j &lt; sizeof *aptr / sizeof *aptr[0]; j++) aptr[i][j] = ...; } return aptr; } </code></pre> <p>Abstraction is a <em>good</em> thing. </p> <p>I've put up several iterations of this table before; you might find it handy.</p> <pre> Declaration: T a[N]; Expression Type Decays To Value ---------- ---- --------- ----- a T [N] T * Address of first element in a &a T (*)[N] n/a Address of a (same value as above, but different type) *a T n/a Same as a[0] a[i] T n/a Value at index i &a[i] T * n/a Address of value at index i sizeof a size_t Total number of bytes in a (N * sizeof T) sizeof a / sizeof *a size_t n/a Number of elements in a (N) Declaration: T a[N][M]; Expression Type Decays To Value ---------- ---- --------- ----- a T [N][M] T (*)[M] Address of first element in a[0] &a T (*)[N][M] n/a Address of a (same value as above, but different type) *a T [M] T * Same as a[0] a[i] T [M] T * Address of first element in array at index i &a[i] T (*)[M] n/a Address of array at index i (same value as above, but different type) *a[i] T n/a Same as a[i][0] a[i][j] T n/a Value at a[i][j] &a[i][j] T * n/a Address of value at index i,j sizeof a size_t n/a Total number of bytes in a (N * M * sizeof T) sizeof a / sizeof *a size_t n/a Number of subarrays in a (N) sizeof a[i] size_t n/a Total number of bytes in a[i] (M * sizeof T) sizeof a[i] / sizeof *a[i] size_t n/a Number of elements in a[i] (M) </pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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