allocaton of memory
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();
开发者_C百科 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;
}
in insert_rear when the function calls getnode it goes to the above function, and it creates a node, while debugging using gdb when i did
(gdb) p temp
$7 = (struct classifier *) 0x8d8f080
(gdb) p &temp
$8 = (struct classifier **) 0xbff9cb04
what is the difference between the two.
In the
p &temp
you are printing the stack address for the variable temp. With
p temp
you are printing the value of temp (which is the address of the allocated memory returned by getnode()
You can have multiple indirections:
temp
has received a pointer address from getnode()
that points to a node memory allocated by malloc
.
&temp
is the address of the memory that store this address (that points to the node).
Basically you have
&temp ---(points to)---> Memory X ***temp*** ---(points to)---> Memory Y ****MEMORY from malloc of "type" node****
So if you use *temp
you are accessing the node. And if you are using *(&temp)
(don't know if it's valid syntax...) but you would be accessing temp
which stores the address where the node is stored.
精彩评论