<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Help with LINKED LIST please?</title>
	<atom:link href="http://www.massemailinglist.com/2010/06/help-with-linked-list-please/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.massemailinglist.com/2010/06/help-with-linked-list-please/</link>
	<description>mass email list</description>
	<pubDate>Fri, 18 May 2012 14:58:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
		<item>
		<title>By: Tizio 008</title>
		<link>http://www.massemailinglist.com/2010/06/help-with-linked-list-please/#comment-2302</link>
		<dc:creator>Tizio 008</dc:creator>
		<pubDate>Tue, 29 Jun 2010 15:11:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.massemailinglist.com/2010/06/help-with-linked-list-please/#comment-2302</guid>
		<description>absolutely not.
in the first case, you're nullifying just the "local copy" of a variable head, which is a pointer to a node... but anyway, you're modifying just the local copy of a pointer; this do not affect the passed in pointer.

the second case it's the right one: you indeed pass a pointer (to a pointer to node), and you use that pointer to modify the "original" variable.

maybe you can see it more easily scaling down; let us suppose you have a function like

void myfunc(int a)
{
  a = 10;
}

/*. ... */
int p;
myfunc(p);

this func does nothing at the "original" variable (p) passed: it modify the local copy which it's discarded when the func ends.

if you want to modify the original, you must do

void myfunc(int *a)
{
  *a = 10;
}

/** ... */
int p;
myfunc(&#038;p);

now, instead of the value ("content") of the integer p, you pass the pointer to the memory area where the value is stored.
the function see that pointer as a pointer to a integer (int * in the C syntax).
now we can dereference the pointer to modify directly the memory area where p is stored, and this is what we do with
*a = 10;
the pointer a is used to "reach" the memory area of p.
the local variable now is just a pointer... we can modify it of course, just for fun since it won't do anything at all

void myfunc(int *a)
{
   *a = 10; // modify what's pointed by a, i.e. p...
   a = (int *)0x40008000; // modify the pointer...
  // the pointer it's a local variable, so nothing special
  // happens but...
   *a = 20;
   // of course now the pointer is "invalid", i.e. pointing
   // to something we maybe do not own, so the previous
  // line cause an error: we are deferencing the modified
  // copy of our variable (which by the way is a pointer)
}

instead of int, you can put anything else; in particular, another pointer, e.g. if we have

struct node *head;

to modify this from within a function we must pass the pointer, &#038;head... which gives as the "odd" type struct node **head, i.e. a pointer which point to a variable which is a pointer to a struct node.</description>
		<content:encoded><![CDATA[<p>absolutely not.<br />
in the first case, you&#8217;re nullifying just the &#8220;local copy&#8221; of a variable head, which is a pointer to a node&#8230; but anyway, you&#8217;re modifying just the local copy of a pointer; this do not affect the passed in pointer.</p>
<p>the second case it&#8217;s the right one: you indeed pass a pointer (to a pointer to node), and you use that pointer to modify the &#8220;original&#8221; variable.</p>
<p>maybe you can see it more easily scaling down; let us suppose you have a function like</p>
<p>void myfunc(int a)<br />
{<br />
  a = 10;<br />
}</p>
<p>/*. &#8230; */<br />
int p;<br />
myfunc(p);</p>
<p>this func does nothing at the &#8220;original&#8221; variable (p) passed: it modify the local copy which it&#8217;s discarded when the func ends.</p>
<p>if you want to modify the original, you must do</p>
<p>void myfunc(int *a)<br />
{<br />
  *a = 10;<br />
}</p>
<p>/** &#8230; */<br />
int p;<br />
myfunc(&#038;p);</p>
<p>now, instead of the value (&#8221;content&#8221;) of the integer p, you pass the pointer to the memory area where the value is stored.<br />
the function see that pointer as a pointer to a integer (int * in the C syntax).<br />
now we can dereference the pointer to modify directly the memory area where p is stored, and this is what we do with<br />
*a = 10;<br />
the pointer a is used to &#8220;reach&#8221; the memory area of p.<br />
the local variable now is just a pointer&#8230; we can modify it of course, just for fun since it won&#8217;t do anything at all</p>
<p>void myfunc(int *a)<br />
{<br />
   *a = 10; // modify what&#8217;s pointed by a, i.e. p&#8230;<br />
   a = (int *)0&#215;40008000; // modify the pointer&#8230;<br />
  // the pointer it&#8217;s a local variable, so nothing special<br />
  // happens but&#8230;<br />
   *a = 20;<br />
   // of course now the pointer is &#8220;invalid&#8221;, i.e. pointing<br />
   // to something we maybe do not own, so the previous<br />
  // line cause an error: we are deferencing the modified<br />
  // copy of our variable (which by the way is a pointer)<br />
}</p>
<p>instead of int, you can put anything else; in particular, another pointer, e.g. if we have</p>
<p>struct node *head;</p>
<p>to modify this from within a function we must pass the pointer, &#038;head&#8230; which gives as the &#8220;odd&#8221; type struct node **head, i.e. a pointer which point to a variable which is a pointer to a struct node.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

