node *getnode()
{
node *x;
x = (node*)malloc(sizeof(node));
if (x==NULL)
{
printf("no memory \n");
exit(1);
}
return x;
}
*insert_rear(int item ,node *first)
{
node *temp;
node *cur;
temp = getnode();
temp -> data = item;
temp -> next = NULL;
if (first == NULL)
return temp;
cur = first;
while(cur -> next != NULL)
{
cur = cur -> next;
}
cur -> next = temp;
return first;
}
在insert_rear中,当函数调用getnode时,它将转到上述函数,并创建一个节点,而当我使用gdb进行调试时
(gdb) p temp
$7 = (struct classifier *) 0x8d8f080
(gdb) p &temp
$8 = (struct classifier **) 0xbff9cb04
两者有什么区别。
最佳答案
在里面
p &temp
您正在打印变量temp的堆栈地址。
用
p temp
您正在打印temp的值(这是getnode()返回的已分配内存的地址
关于c - 内存分配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5243527/