title: How to explain C pointers (declaration vs. unary operators) to a beginner?
body: <p>I have had the recent pleasure to explain pointers to a C programming beginner and stumbled upon the following difficulty. It might not seem like an issue at all if you already know how to use pointers, but try to look at the following example with a clear mind:</p>
  <pre><code>int foo = 1;
  int *bar = &amp;foo;
  printf(&quot;%p\n&quot;, (void *)&amp;foo);
  printf(&quot;%i\n&quot;, *bar);
  </code></pre>
  <p>To the absolute beginner the output might be surprising. In line 2 he/she had just declared *bar to be &amp;foo, but in line 4 it turns out *bar is actually foo instead of &amp;foo!</p>
  <p>The confusion, you might say, stems from the ambiguity of the * symbol: In line 2 it is used to declare a pointer. In line 4 it is used as an unary operator which fetches the value the pointer points at. Two different things, right?</p>
  <p>However, this &quot;explanation&quot; doesn't help a beginner at all. It introduces a new concept by pointing out a subtle discrepancy. This can't be the right way to teach it.</p>
  <p>So, how did Kernighan and Ritchie explain it?</p>
  <blockquote>
  <p>The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to. […]</p>
  <p>The declaration of the pointer ip, <code>int *ip</code> is intended as a mnemonic; it says that the expression <code>*ip</code> is an int. <strong>The syntax of the declaration for a variable mimics the syntax of expressions in which the variable might appear</strong>.</p>
  </blockquote>
  <p><code>int *ip</code> should be read like &quot;<code>*ip</code> will return an <code>int</code>&quot;? But why then doesn't the assignment after the declaration follow that pattern? What if a beginner wants to initialize the variable? <code>int *ip = 1</code> (read: <code>*ip</code> will return an <code>int</code> and the <code>int</code> is <code>1</code>)  won't work as expected. The conceptual model just doesn't seem coherent. Am I missing something here?</p>
  <hr />
  <p>Edit: <strong>It tried to <a href="https://stackoverflow.com/a/27504628/412787">summarize the answers here</a></strong>.</p>