ポインターを渡すことは、基本的にポインターを値として渡すようなものです..関数内で内部的にポインターを変更しても、ポインターの実際の値は変更されません..しかし、関数内で実際のポインター自体にアクセスする必要がある場合は、ポインターへのポインターの概念を考え出しています。これは私の理解です..
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(...) でポインターへのポインターを使用していない理由と、そこでポインターを変更すると思ったのは混乱していますか?