分配二维数组的方式任性? [英] Freaky way of allocating two-dimensional array?
问题描述
在一个项目,有人推这一行:
In a project, somebody pushed this line:
double (*e)[n+1] = malloc((n+1) * sizeof(*e));
哪个假想创建的二维阵列(N + 1)*(N + 1)的两倍。
Which supposedly creates a two-dimensional array of (n+1)*(n+1) doubles.
的据说的,我说,因为到目前为止,没有人,我问能告诉我这是什么呢,正好,也不在那里它源自或者为什么它应该工作(据称,这样做,但我M尚未购买它)。
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).
也许我缺少明显的东西,但我AP preciate它,如果有人能高于线向我解释。因为就个人而言,我会感觉更好,如果我们可以使用的东西,我们其实都明白。
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.
推荐答案
变量电子
是一个指向 N + 1的阵列
类型的元素双击
。
The variable e
is a pointer to an array of n + 1
elements of type double
.
使用引用操作上电子
为您提供了基本型电子
这是<$的数组C $ C> N + 1 类型的元素双击
Using the dereference operator on e
gives you the base-type of e
which is " array of n + 1
elements of type double
".
的的malloc
通话只是将基本型电子商务
(如上所述),并得到它的大小,乘它由 N + 1
,而大小传递给的malloc
功能。从本质上讲分配的的n + 1
阵列 N
的+ 1
元素双数组code>。
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
.
这篇关于分配二维数组的方式任性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!