title: C++ - Array of struct pointers
body: <p>I have a small program in which I have two structs:
   Person - Consists of id, and basic methods
   Ppl - Consists of an array of people with some methods to operate on the array.</p>
  
  <pre><code>struct Person {
   const int id;
   Person();
   Person(int a);
  };
  
  Person::Person(int a) : id(a) {}
  </code></pre>
  
  <p>This is the Person struct with its methods. </p>
  
  <pre><code>const int MAX = 5;
  </code></pre>
  
  <p>Sets limit on array length</p>
  
  <pre><code>struct Ppl {
   static int current;   //Keeps track of current position in array
   Person *ppls[MAX];    //Creates an array of Person structure pointers
   void add(int a);      //Adds a person with id to the next available position
   //void remove(int b);
   int searchid(int c);  //Searches ppls for an id c.
   Ppl();                //Constructor
  };
  
  int Ppl::current = 0;  //Initializing static variable
  
  void Ppl::add(int a) {
   int ret = searchid(a);          //Determine if id a already exists in ppls
   //cout &lt;&lt; "Ret: " &lt;&lt; ret &lt;&lt; endl;
   if (ret &gt; MAX) {                //If value returned is &gt; MAX, value exists
    cout &lt;&lt; "User " &lt;&lt; a &lt;&lt; " already exists" &lt;&lt; endl;
   } else {                        //else, add it to the next available spot
    Person p(a);
    ppls[current] = &amp;p;
    cout &lt;&lt; "Added user: " &lt;&lt; a &lt;&lt; " at index: " &lt;&lt; current &lt;&lt; endl;
    current++;
   }
  }
  
  Ppl::Ppl() {
   current = 0;
   int i = 0;
   while (i &lt; MAX) {    //Sets all Person pointers to NULL on initialization
    ppls[i] = NULL;
    cout &lt;&lt; "ppls[" &lt;&lt; i &lt;&lt; "] is NULL" &lt;&lt; endl;
    i++;
   }
  }
  
  int Ppl::searchid(int c) {
   int i = 0;
   bool found = false;
   while(i &lt; MAX) {
    if (ppls[i] == NULL) {  //If NULL, then c wasn't found in array because
     break;                 //there is no NULL between available spots.
    } else {
      if (ppls[i]-&gt;id == c) {
       found = true;
     }
    }
    i++;
   }
   if (found == true) {
    return 10;     //A number higher than MAX
   } else {
    return 1;      //A number lower than MAX
   }
  }
  </code></pre>
  
  <p>The main function is:</p>
  
  <pre><code>int main() {
   Ppl people;
   people.add(21);
   cout &lt;&lt; people.ppls[0]-&gt;id &lt;&lt; endl;
   people.add(7);
   cout &lt;&lt; people.ppls[0]-&gt;id &lt;&lt; " ";
   cout &lt;&lt; people.ppls[1]-&gt;id &lt;&lt; endl;
   people.add(42);
   cout &lt;&lt; people.ppls[0]-&gt;id &lt;&lt; " ";
   cout &lt;&lt; people.ppls[1]-&gt;id &lt;&lt; " ";
   cout &lt;&lt; people.ppls[2]-&gt;id &lt;&lt; endl;
   people.add(21);
   cout &lt;&lt; people.ppls[0]-&gt;id &lt;&lt; " ";
   cout &lt;&lt; people.ppls[1]-&gt;id &lt;&lt; " ";
   cout &lt;&lt; people.ppls[2]-&gt;id &lt;&lt; " ";
   cout &lt;&lt; people.ppls[3]-&gt;id &lt;&lt; endl;
  }
  </code></pre>
  
  <p>The output that I get is:</p>
  
  <pre><code>ppls[0] is NULL
  ppls[1] is NULL
  ppls[2] is NULL
  ppls[3] is NULL
  ppls[4] is NULL
  Added user: 21 at index: 0
  21
  Added user: 7 at index: 1
  7 0
  Added user: 42 at index: 2
  42 0 0
  Added user: 21 at index: 3
  21 0 0 0
  </code></pre>
  
  <p>Why is it adding all new entries to the beginning of the array while keeping the rest NULL?
  Why isn't it detecting that 21 was already added.</p>
  
  <p>I have been going crazy trying to figure this out. Any help would be much appreciated. 
  Thanks a lot community.</p>
  
  <p><strong>EDIT</strong>
  I have fixed it so that it adds the elements to the array and recognizes when an id exists.</p>
  
  <p>I made changes to the Ppl struct by adding a destructor:</p>
  
  <pre><code>Ppl::~Ppl() {
   int i = 0;
   while (i &lt; MAX) {
    delete ppls[i];
    i++;
   }
  }
  </code></pre>
  
  <p>and by changing the add method.</p>
  
  <pre><code>void Ppl::add(int a) {
   int ret = searchid(a);
   //cout &lt;&lt; "Ret: " &lt;&lt; ret &lt;&lt; endl;
   if (ret &gt; MAX) {
    cout &lt;&lt; "User " &lt;&lt; a &lt;&lt; " already exists" &lt;&lt; endl;
   } else {
    **Person *p = new Person(a);
    ppls[current] = p;**
    cout &lt;&lt; "Added user: " &lt;&lt; a &lt;&lt; " at index: " &lt;&lt; current &lt;&lt; endl;
    current++;
   }
  }
  </code></pre>
  
  <p>So the output now is</p>
  
  <pre><code>ppls[0] is NULL
  ppls[1] is NULL
  ppls[2] is NULL
  ppls[3] is NULL
  ppls[4] is NULL
  Added user: 21 at index: 0
  21
  Added user: 7 at index: 1
  21 7
  Added user: 42 at index: 2
  21 7 42
  User 21 already exists
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>What is a segmentation fault and how can I fix it?
  Thanks again</p>