Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First Its not "the address of the address." Its the address of a pointer variable. Ex: if you pass the address of an <code>int</code> variable <code>n</code> that contains zero, you're not passing the address of zero; you're passing the address of a variable (in this case an <code>int</code> variable, in your case a pointer-variable). Variables have addresses in memory. The parameter in this case is the address of a variable that happens to be a pointer-variable, the head of your list.</p> <p>Regarding why one does this? Simple. All variables in C (arrays via pointer-decay not withstanding) are passed by <strong>value</strong>. If you want to modify something by reference (address), then the "value" you need to pass must be an address and the formal parameter that receives it must be a pointer-type. In short, you make the "value" being passed a memory address rather than just a basic scaler value. The function then uses this (via a formal pointer parameter) to store data accordingly. Think of it as saying "put that thing I wanted at "this" memory address."</p> <p>As a short example, suppose you wanted to run through a file, appending every character within into a <strong><em>forward</em></strong> linked list of nodes. You would <em>not</em> use an append-method like the one you have (see <a href="http://en.wikipedia.org/wiki/Schlemiel_the_Painter%27s_algorithm" rel="nofollow">The Painter's Algorithm</a> for why). See if you can follow this code, which uses a pointer-to-pointer, but no function call.</p> <pre><code>typedef struct node { char ch; struct node *next; } node; node *loadFile(const char *fname) { node *head = NULL, **next = &amp;head; FILE *fp = fopen(fname, "r"); if (fp) { int ch; while ((ch = fgetc(fp)) != EOF) { node *p = malloc(sizeof(*p)); p-&gt;ch = ch; *next = p; next = &amp;p-&gt;next; } *next = NULL; fclose(fp); } return head; } </code></pre> <p>Stare at that awhile and see if you can understand how the pointer-to-pointer <code>next</code> is used to always populate the next linked node to be added to the list, starting with the head-node.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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