title: Pointer scope in linked list function cpp
body: <pre><code>struct Entry {
      std::string name, phone;
      Entry *next;
  };
  
  Entry * getNewEntry() {
      std::cout &lt;&lt; "Enter name (RETURN to quit): ";
      std::string name;
      std::getline(std::cin, name);
      if (name == "") return NULL;
      Entry *newOne = new Entry; //allocates a new entry struct obj in the heap
      newOne-&gt;name = name;
      std::cout &lt;&lt; "Enter phone number: ";
      std::string number;
      std::getline(std::cin, number);
      newOne-&gt;phone = number;
      newOne-&gt;next = NULL; //in this function pointer to next entry is NULL
      return newOne;
  }
  
  void prepend(Entry *ent, Entry *first) {
      ent-&gt;next = first;
      *first = *ent;
  }
  
  Entry * buildAddressBook() {
      Entry *listHead = NULL;
      while (true) {
          Entry *newOne = getNewEntry();
          if (!newOne) break;
          prepend(newOne, listHead);
      }
      return listHead;
  }
  </code></pre>
  
  <p>Why does not work this line in <code>prepend()</code>?:</p>
  
  <pre><code>*first = *ent;
  </code></pre>
  
  <p>I know I could pass first by reference and get it to work, but if I am referencing first and setting that equal to the struct that ent points to why doesn't this work? Even if the pointer vars are passed by value they still point to the same struct?</p>