In a project, somebody pushed this line:
double (*e)[n+1] = malloc((n+1) * sizeof(*e));
Which supposedly creates a two-dimensional array of (n+1)*(n+1) doubles.
Supposedly, I say, because so far, nobody I asked could tell me what this does, exactly, nor where it originated from or why it should work (which allegedly, it does, but I'm not yet buying it).
Perhaps I'm missing something obvious, but I'd appreciate it if somebody could explain above line to me. Because personally, I'd feel much better if we'd use something we actually understand.
The variable e
is a pointer to an array of n + 1
elements of type double
.
Using the dereference operator on e
gives you the base-type of e
which is " array of n + 1
elements of type double
".
The malloc
call simply takes the base-type of e
(explained above) and gets its size, multiplies it by n + 1
, and passing that size to the malloc
function. Essentially allocating an array of n + 1
arrays of n + 1
elements of double
.