Note that there are some explanatory texts on larger screens.

plurals
  1. POpointer of a pointer in linked list append
    primarykey
    data
    text
    <p>I normally program in python. To increase performance of my simulations, I am learning C. I have a problem to understand the use of a pointer of a pointer when implementing the append function to a linked list. This is an excerpt of the code from my book (Understanding Pointers in C by Kanetkar).</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; struct node{ int data; struct node *link; }; int main(){ struct node *p; //pointer to node structure p = NULL; //linked list is empty append( &amp;p,1); return 0; } append( struct node **q, int num){ struct node *temp, *r; //two pointers to struct node temp = *q; if(*q == NULL){ temp = malloc(sizeof(struct node)); temp -&gt; data = num; temp -&gt; link = NULL; *q = temp; } else{ temp = *q; while( temp -&gt; link != NULL) temp = temp -&gt; link; r = malloc(sizeof(struct node)); r -&gt; data = num; r -&gt; link = NULL; temp -&gt; link = r; } } </code></pre> <p>In this code, I pass the double pointer **q to the append function. I get that this is the adress of the address, i.e. the adress of NULL in this case. </p> <p>I just don't get <strong><em>why</em></strong> one does it like this. Would it not be valid to remove one * operator from everything in the append() function and simply pass the adress of NULL (i.e. p instead of &amp;p) to the append() function?</p> <p>I have googled this question. The answers are either too hard to understand (since I'm just a C beginner) or too plain. I'm thankful for any hints, comments or links where I can read up about this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload