C 指向结构数组并获取内存的指针
链接是指向节点的指针C 指向结构数组并获取内存的指针,c,pointers,C,Pointers,链接是指向节点的指针 typedef struct node * link; 在main()中,我有以下代码(config->m只是一个整数): 但是我的友好邻域编译器告诉我赋值中的不兼容类型 int i; // we give the first node a val of zero na[0].val = 0; // and a null next pointer na[0].next = 0 ; 我知道我可以用 struct node
typedef struct node * link;
在main()中,我有以下代码(config->m只是一个整数):
但是我的友好邻域编译器告诉我赋值中的不兼容类型
int i;
// we give the first node a val of zero
na[0].val = 0;
// and a null next pointer
na[0].next = 0 ;
我知道我可以用
struct node heads[config->m];
但我想用指针来做这件事
和往常一样,有人会问我这是否是家庭作业的一部分,答案是肯定的(有点)。但这段特定的代码与实际的赋值无关;这是为了我自己的启示。但是谢谢你的提问:|
link heads[config->m];
link buffer = malloc(sizeof(struct node) * config->m);
for(i = 0; i < config->m; i++)
heads[i] = &buffer[i];
....
free(buffer);
现在我们添加我们的include和config->m:
#include <stdio.h>
#include <stdlib.h>
// your config->m
const int m = 10 ;
现在我们声明一个指向节点的指针:
// na is a node pointer
struct node* na;
和malloc up m个节点。malloc返回数组的地址,它也是数组中第一个节点的地址。我们将na设置为malloc返回的地址:
na = malloc(sizeof(struct node) * m);
现在我们将使用na,一个指针,就像它是一个数组一样。这是因为C将array[offset]
定义为*(array+offset*sizeof(element))
现在,我们将遍历阵列的其余部分,并将每个节点的位置设置为阵列中前一个节点的旁边:
for(i = 1; i < m; i++) {
na[i].val = i ;
// na[ offset ] is *(na + offset)
// we don't want to dereference,
// we want the address, so we use the
// address-of operator ("&")
na[i].next = &na[ i - 1 ];
}
我们将打印每个节点的地址。它应该是它的next
节点指针+sizeof(struct node)
的地址,因为每个节点都是(在数组中)它在列表中的下一个之后的节点(列表是“反向”数组)
我们将其强制转换为char*以获得以字节为单位的结果。如果不进行强制转换,我们将以truct node*
为单位获得结果(该单位应始终为1)
在我的系统上,它提供以下输出:
sizeof( struct node ) = 48
val 9, address of current 0x804a1b8, address of current->next 0x804a188, distance from next: in bytes 48, in struct nodes 1
val 8, address of current 0x804a188, address of current->next 0x804a158, distance from next: in bytes 48, in struct nodes 1
val 7, address of current 0x804a158, address of current->next 0x804a128, distance from next: in bytes 48, in struct nodes 1
val 6, address of current 0x804a128, address of current->next 0x804a0f8, distance from next: in bytes 48, in struct nodes 1
val 5, address of current 0x804a0f8, address of current->next 0x804a0c8, distance from next: in bytes 48, in struct nodes 1
val 4, address of current 0x804a0c8, address of current->next 0x804a098, distance from next: in bytes 48, in struct nodes 1
val 3, address of current 0x804a098, address of current->next 0x804a068, distance from next: in bytes 48, in struct nodes 1
val 2, address of current 0x804a068, address of current->next 0x804a038, distance from next: in bytes 48, in struct nodes 1
val 1, address of current 0x804a038, address of current->next 0x804a008, distance from next: in bytes 48, in struct nodes 1
val 0, address of current 0x804a008, address of current->next (nil),
不,你需要回路。您的heads数组本质上是一个二维数组。您至少需要两次分配。第一个是指针数组:
link * heads = (link*)malloc (config->m * sizeof (link));
第二个是heads数组的每个成员指向的内存:
link buf = (link)malloc(sizeof(struct node) * config->m);
for(i = 0; i < config->m; i++)
heads[i] = &buf[i];
嗯,我想这回答了我的问题。谢谢。那不应该是heads[i]=&buf[i];在for循环中?您希望头指针具有节点的地址,对吗?谢谢tpdi。实际上,原始heads数组应该是指针的link*(或node**)数组。因此发生了错误。我喜欢C,直到我在节点结构中添加了一些填充,我的测试程序才崩溃。实际上这里还有一个问题:。我完全同意第一个答案。typedef blah*这件事打乱了我的间接感;)。我最初出于同样的原因犯了同样的错误。我从未见过像这样使用内存缓冲区。我认为,如果您可以在调用中获得所有内存,那么最好避免对malloc进行大量调用。谢谢。不过我把事情搞砸了
for(i = 1; i < m; i++) {
na[i].val = i ;
// na[ offset ] is *(na + offset)
// we don't want to dereference,
// we want the address, so we use the
// address-of operator ("&")
na[i].next = &na[ i - 1 ];
}
struct node* current = &na[ m - 1 ];
while( current ) {
printf( "val %i, address of current %p, ", current->val, current) ;
printf( " address of current->next %p, ", current->next ) ;
if( current->next ) {
printf( " distance from next: ");
printf( "in bytes %i, ",
( (char*) current) - (char*) current->next ) ;
printf( " in struct nodes %i", current - current->next ) ;
}
printf( "\n" );
current = current->next;
}
return 0;
}
sizeof( struct node ) = 48
val 9, address of current 0x804a1b8, address of current->next 0x804a188, distance from next: in bytes 48, in struct nodes 1
val 8, address of current 0x804a188, address of current->next 0x804a158, distance from next: in bytes 48, in struct nodes 1
val 7, address of current 0x804a158, address of current->next 0x804a128, distance from next: in bytes 48, in struct nodes 1
val 6, address of current 0x804a128, address of current->next 0x804a0f8, distance from next: in bytes 48, in struct nodes 1
val 5, address of current 0x804a0f8, address of current->next 0x804a0c8, distance from next: in bytes 48, in struct nodes 1
val 4, address of current 0x804a0c8, address of current->next 0x804a098, distance from next: in bytes 48, in struct nodes 1
val 3, address of current 0x804a098, address of current->next 0x804a068, distance from next: in bytes 48, in struct nodes 1
val 2, address of current 0x804a068, address of current->next 0x804a038, distance from next: in bytes 48, in struct nodes 1
val 1, address of current 0x804a038, address of current->next 0x804a008, distance from next: in bytes 48, in struct nodes 1
val 0, address of current 0x804a008, address of current->next (nil),
link * heads = (link*)malloc (config->m * sizeof (link));
link buf = (link)malloc(sizeof(struct node) * config->m);
for(i = 0; i < config->m; i++)
heads[i] = &buf[i];
free(heads);
free(buf);