问题描述
请考虑以下内容:
typedef struct node
{
int number;
struct node *left;
struct node *right;
} node;
node test[511];
node *test1 = malloc(511 * sizeof(node));
node (*test2)[511] = malloc(511 * sizeof(node));
以下是正确的:
- test是511个节点的数组.我们可以使用test [i]访问每个节点,其中i是我们想要的节点
- test1是一个指针,其中我们为511个节点分配了空间.我们可以使用test1 [i] 访问每个节点
test和test1基本相同吗,除了test在堆栈上,而test1在堆上?
so are test and test1 basically the same except test is on the stack and test1 on the heap?
test2看起来像是指向数组的指针. malloc是否为每个指针或整个数组分配511 * sizeof(node)?如果稍后可以使用test2 [i]访问每个节点,并且前者可以执行以下操作:
test2 looks like a pointer to an array. does malloc allocate 511 * sizeof(node) to each pointer or to the entire array? if the later then can we access each node with test2[i] and if the former can do the following:
node (*test2)[511] = malloc(sizeof(node));
然后使用test2 [i]访问每个节点?
and then access each node with test2[i]?
通常* test1和(* test2)有什么区别?
in general whats the difference between *test1 and (*test2)?
推荐答案
是的,可以自由地说.带有免责声明,它们是不同的类型.
Yes, freely speaking, we can say that. With a disclaimer, these are different types.
再说一次,我们可以对每个指针说一遍,在这种情况下,它也恰好是整个数组,因为您只分配了511个节点的1个块.
Again, freely speaking we can say to each pointer, wich in this case also happens to be the entire array, as you only allocate 1 block of 511 nodes.
这是指向511数组的指针,因此,您应该仅向其分配511 * sizeof(node)
的倍数的内存块.您可以为其分配类似以下内容的
This is a pointer to array of 511, as such you should only assing to it blocks of memory that are multiples of 511 * sizeof(node)
. You could assign to it something like:
node (*test2)[511] = malloc(sizeof(node) * 511 * 5);
在这种情况下,您将拥有5个node (*test2)[511]
的数组.您可以将其等同于node test2[5][511]
,因为访问符号是相同的.
In which case you would have an array of 5 node (*test2)[511]
. You can equate this to node test2[5][511]
as the access notation is the same.
node (*test2)[511] = malloc(sizeof(node));
然后使用test2[i]
访问每个节点?
and then access each node with test2[i]
?
此分配不正确. test2[0]
指向511个节点的第一个块,特别是511个节点的每个块的第一个元素,test2[1]
指向下一个511个节点的块的第一个元素,您不能使用此指针访问单个个节点,但511个节点的每个块中的每个第一个节点除外.
This allocation is not correct. test2[0]
points to the first block of 511 nodes, specifically to the first element of each block of 511 nodes, test2[1]
points to the first element of the next block of 511 nodes, you can't use this pointer to access individual nodes, with the exception of each first node of every block of 511 nodes.
必须使用例如test2[0][1]
来访问511个节点的第一个块的第二个节点(索引1).
The access to individual nodes must be made with, for instance test2[0][1]
, for the second node (index 1) of the first block of 511 nodes.
因此,同样,分配必须是511 * sizeof(node)
倍数的块.
So, again, the allocation must be of blocks of multiples of 511 * sizeof(node)
.
就是这样,test1
是指向node
的指针,test2
是指向511个节点的数组的指针.前面的解释应该引起不同.
That's it, test1
is a pointer to node
, test2
is a pointer to array of 511 nodes. The previous explanations should make the difference noted.
test1
用于访问节点数组每个块的任何成员,test2
用于访问511个节点的每个块.
test1
is used to access any member of each block of the node array, test2
is used to access each block of 511 nodes.
这篇关于数组和指针的语法混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!