用以下的代码作为示例,解释。
struct Node* head = NULL;
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node = new Node();
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
在这里每次修改的都是指针指向的地址。
1)在函数里想要修改外部一个变量的值,实参是通过引用传递,也就是传地址,传的是变量的地址。
形参的接收方式
eg :int modify(int * i)
2)在函数里想要修改一个指针时,那么也通过引用传递, 还是传地址,只是在这里,传递的是一个指向指针的地址。
总结: 在上面的代码中,在Push节点时, *head_ref 都要改变,所以形参接收的是指向指针的地址。