NOTE: Deviating from the requested array-of-pointers-to-rows syntax and pointing to the rows directly in the array, as proposed by @Someprogrammerdude, allows to obtain the same result, but with one less indirection and with a more clear access syntax.
direct array of rows solution
definition
unsigned jagged_row0[] = { 0, 1, 99 };
unsigned jagged_row1[] = { 1, 2, 3, 4, 5, 6, 99 };
unsigned *jagged[] = (unsigned *[]){ jagged_row0, jagged_row1 };
or in general:
type jagged_row0[] = { ... };
type jagged_row1[] = { ... };
...
type *jagged[] = (type *[]){ jagged_row0, jagged_row1, ... };
declaration
extern unsigned *jagged[];
or in general:
extern type *jagged[];
usage
unsigned v_i_j = jagged[i][j];
or in general:
type v_i_j = jagged[i][j];
original answer
The following solution addresses the definition given in the cited answer by @FaisalVasi, where the jagged array stores explicit pointers to the jagged rows.
definition (in some .c file)
unsigned jagged_row0[] = {0,1};
unsigned jagged_row1[] = {1,2,3};
unsigned (*jagged[])[] = { &jagged_row0, &jagged_row1 }; /* note the ampersand */
/* or alternatively, since compound literals are lvalues ... */
unsigned (*jagged[])[] = { &(int[]){0,1}, &(int[]){1,2,3} };
declaration
extern unsigned (*jagged[])[];
usage
unsigned *jagged_row;
...
jagged_row = *jagged[i];
unsigned v_i_j = jagged_row[j]; /* value at [i][j] */
or more compactly:
unsigned v_i_j = (*jagged[i])[j]; /* value at [i][j] */
explanation
A jagged row is an array of some basic type, in our case an array (of length determined by the static initialization) of unsigned (unsigned[]
), which can be thought of, with some caveats, as a pointer to unsigned (unsigned *
).
With the proposed definition, the jagged array is an array of pointers to jagged rows, which, with the same simplification, can be though of as an array of unsigned **
.
When you index the first dimension, you are getting the pointer to the jagged row (an array), then you have to dereference this pointer to get to the array itself that is the jagged row, than you have to index this array to get to the final value.