title: Pointer Initializing to 0x1, Segmentation Fault
body: <p>I've searched around and haven't found the exact problem I'm having or a potential solution to it.</p>
  
  <p>I have a program that uses pointers for a linked list and I've narrowed down the problem to my pointers when they get initialized.  Below is the relevant code:</p>
  
  <pre><code>// Include header files
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int counter = 0;
  
  // Structure prototype
  struct numberList {
      int value;
  
      struct numberList *leftPtr = NULL;
      struct numberList *rightPtr = NULL;
  };
  
  // Function prototypes
  struct numberList *getNextNumber(); // Reads next number from input file
  void addNumToList(numberList *, numberList *, numberList *, numberList *); // Takes next user import and adds it to the current linked list
  void printPointerValues(numberList *, numberList *, numberList *, numberList *); // Used for debug purposes
  
  // Main
  int main() {
      // Define variables
      string filename;
      int number;
      struct numberList *firstPtr, *midPtr, *newPtr, *lastPtr;
  
      printPointerValues(newPtr, firstPtr, midPtr, lastPtr);
  
      return 0;
  }
  
  
  
  void printPointerValues(numberList *newPtr, numberList *firstPtr, numberList *midPtr, numberList *lastPtr) {
      cout &lt;&lt; "\n";
      cout &lt;&lt; "counter:\t" &lt;&lt; counter++ &lt;&lt; endl;
      cout &lt;&lt; "\n";
  //  cout &lt;&lt; "first value:\t" &lt;&lt; firstPtr-&gt;value &lt;&lt; endl;
      cout &lt;&lt; "first address:\t" &lt;&lt; firstPtr &lt;&lt; endl;
      cout &lt;&lt; "first left:\t" &lt;&lt; firstPtr-&gt;leftPtr &lt;&lt; endl;
      cout &lt;&lt; "first right:\t" &lt;&lt; firstPtr-&gt;rightPtr &lt;&lt; endl;
      cout &lt;&lt; "\n";
  //  cout &lt;&lt; "mid value:\t" &lt;&lt; midPtr-&gt;value &lt;&lt; endl;
      cout &lt;&lt; "mid address:\t" &lt;&lt; midPtr &lt;&lt; endl;
      cout &lt;&lt; "mid left:\t" &lt;&lt; midPtr-&gt;leftPtr &lt;&lt; endl;
      cout &lt;&lt; "mid right:\t" &lt;&lt; midPtr-&gt;rightPtr &lt;&lt; endl;
      cout &lt;&lt; "\n";
  //  cout &lt;&lt; "last value:\t" &lt;&lt; lastPtr-&gt;value &lt;&lt; endl;
      cout &lt;&lt; "last address:\t" &lt;&lt; lastPtr &lt;&lt; endl;
  //  cout &lt;&lt; "last left:\t" &lt;&lt; lastPtr-&gt;leftPtr &lt;&lt; endl;
  //  cout &lt;&lt; "last right:\t" &lt;&lt; lastPtr-&gt;rightPtr &lt;&lt; endl;
      cout &lt;&lt; "\n";
  //  cout &lt;&lt; "new value:\t" &lt;&lt; newPtr-&gt;value &lt;&lt; endl;
      cout &lt;&lt; "newPtr address:\t" &lt;&lt; newPtr &lt;&lt; endl;
      cout &lt;&lt; "newPtr left:\t" &lt;&lt; newPtr-&gt;leftPtr &lt;&lt; endl;
      cout &lt;&lt; "newPtr right:\t" &lt;&lt; newPtr-&gt;rightPtr &lt;&lt; endl;
      cout &lt;&lt; "\n\n\n";
  }
  </code></pre>
  
  <p>I've cut out the rest of the code for the program because I'm currently getting errors with printing out the values of <code>lastPtr-&gt;leftPtr</code> and <code>lastPtr-&gt;rightPtr</code>.</p>
  
  <p>When I run the program as shown above I get:</p>
  
  <pre><code>counter:    0
  
  first address:  0xbfed29bc
  first left: 0xbfed2c41
  first right:    0xbfed2c53
  
  mid address:    0xbfed29b4
  mid left:   0
  mid right:  0xbfed2c36
  
  last address:   0x1
  
  newPtr address: 0x8049653
  newPtr left:    0xc629ffff
  newPtr right:   0x8502fec1
  </code></pre>
  
  <p>When I run the code with the <code>leftPtr-&gt;leftPrt</code> and related bits uncommented in the <code>printPointerValues</code> function I get:</p>
  
  <pre><code>counter:    0
  
  first address:  0xbf8c3ecc
  first left: 0xbf8c5c41
  first right:    0xbf8c5c53
  
  mid address:    0xbf8c3ec4
  mid left:   0
  mid right:  0xbf8c5c36
  
  last address:   0x1
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>The whole point of the function was to debug what was going wrong.  Fortunately it narrowed it down to what is shown above.  The problem is that I don't know why some of the pointers are initializing properly and others aren't.  Any ideas?  Also, I don't know why <code>midPtr-&gt;leftPtr</code> initialized to <code>NULL</code> ( or <code>0</code>) and none of the others did...</p>