传递指针基本上就像将指针作为值传递。函数内部对指针的更改不会修改指针的实际值。但是当我们需要在函数中访问实际指针本身时,我们提出了指向指针概念的指针。这是我的理解。。
struct node
{
int data;
struct node* next;
};
void push(struct node** head_ref, int new_data) // i understand the need of pointer to pointer here, since we are changing the actual value by adding a node..
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void insertAfter(struct node* prev_node, int new_data) // why are we not using pointer to pointer here since even here the pointer data is getting modified..??
{
if (prev_node == NULL)
{
return;
}
struct node* new_node =(struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
int main()
{
struct node* head = NULL;
append(&head, 6);
insertAfter(head->next, 8);
return 0;
}
请澄清..我很困惑为什么我们没有在 InsertAfter(...) 中使用指向指针的指针,以及认为我们在那里更改了指针?