Assignment doesn't work but address of with the dereference operator does? : <p>I've been playing around with C++ (just starting out), and I'm trying to make a program that needs a function to count the lines in C++. However, I've encountered weird behavior where normal assignment doesn't work, but assignment through address of, and then immediate dereference does. Like this:</p>

<pre><code>int countLinesInFile(string fileName){
  char c[32];
  int numLines = 0;
  ifstream file(fileName);

  while(file &gt;&gt; c){
    numLines += 1;
    cout &lt;&lt; "Lines: " &lt;&lt; numLines &lt;&lt; endl;
  }
  return numLines;
}
</code></pre>

<p>Which results in:</p>

<pre><code>Lines: 1
Lines: 1
Lines: 1
Lines: 1
Lines: 1
Lines: 1
</code></pre>

<p>However, when I change <code>numLines += 1</code> to <code>*(&amp;numLines) += 1</code> it magically works:</p>

<pre><code>Lines: 1
Lines: 2
Lines: 3
Lines: 4
Lines: 5
Lines: 6
</code></pre>

<p>For a little background, the file I'm reading is a 6 line file where each line is a 32 bit binary string (equal to zero). When I print out c (with <code>cout &lt;&lt; c</code>) it prints out seemingly correctly. Also, I am aware that this may not be the optimal or correct way of doing read lines from a file, but unless this simply can not possibly work, I am more interested in the underlying mechanics of why this behavior is happening, and what I am doing wrong.</p>
