Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, let's make your types match up. <code>foo()</code>'s type signature says it returns a <code>node</code>, but you are actually trying to return a pointer to <code>node</code>. Let's redefined <code>foo()</code> to be</p> <pre><code>node *foo() { return headPtr; } </code></pre> <blockquote> <p>Will returning it return the address of what the pointer is pointing to or the pointer itself?</p> </blockquote> <p>The line</p> <pre><code>tmpPtr = foo(); </code></pre> <p>will place into the variable <code>tmpPtr</code> the address of what the pointer is pointing to, and <code>tmpPtr</code> will be another variable that exactly matches <code>headPtr</code>. That is to say, two separate pointers that point to the same thing. There are two separate points to make here:</p> <ul> <li><code>tmpPtr</code> and <code>headPtr</code> are two separate variables, and if you re-assign one it will have no effect on the other.</li> <li>Since they currently point to the same node, you <em>could</em> potentially mutate the node that they point to by dereferencing either pointer, assuming they point to an actual node and not <code>NULL</code>.</li> </ul> <p>If your intent is to change what <code>headPtr</code> points to you can either:</p> <ul> <li>change <code>headPtr</code> directly with an assignment like <code>headPtr = something</code>.</li> <li>set up a pointer to <code>headPtr</code> with type <code>node**</code>.</li> </ul> <p>You can set up a pointer to <code>headPtr</code> with something like</p> <pre><code>node **foo() { return &amp;headPtr; } int main() { node **headPtrPtr = foo(); *headPtrPtr = something else; } </code></pre> <p>This will place into <code>headPtrPtr</code> a pointer to <code>headPtr</code>. By dereferencing <code>headPtrPtr</code>, you can make <code>headPtr</code> point to a new 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. 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