リストの先頭にリンクされたリストにノードを挿入するために、このコードがウェブサイト上に見つかりました。ポインタへのポインタを使用するポイントは何ですか?
void push(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
/* 2. put in the data */
new_node->data = new_data;
/* 3. Make next of new node as head */
new_node->next = (*head_ref);
/* 4. move the head to point to the new node */
(*head_ref) = new_node;
}
これは
push(&head, 7);
call-機能である私の質問は、なぜポインタのアドレスを渡すことで長い道を行く、その後関数内のアドレスに値を抽出することです。 は、なぜ我々は単に機能にポインタhead
を渡し、後のような割り当てを行うことはできません -
new_node->next=head_ref;
?
[ジャグ付き配列](https://en.wikipedia.org/wiki/Jagged_array) –
この関数は 'head_ref'の値も変更しているためです。 'head'はポインタ型を持っているので、' head'の値を更新するために 'push'のポインタを渡す必要があります。 –
[this](https://stackoverflow.com/a/897400/971127) – BLUEPIXY