title: How to inspect an individual element in a c++ map using GDB and access members of an object pointed by the element in the map
body: <p>Using GDB, I want to access member variable pointers of an element in a map to inspect values of the object referred by the said pointer.</p>
  
  <p>Consider the below program.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  
  struct Parent
  {
      std::string phone_number;
      Parent(std::string num): phone_number(num){}
  };
  
  struct Student
  {
      int student_id;
      std::string student_name;
      Parent* parent = nullptr;
  
      Student(int id, std::string name):student_id(id), student_name(name) {}
  
  };
  
  int main()
  {
      Parent* p_tim = new Parent("0712323456");
      Parent* p_tom = new Parent("0112122123");
  
      Student ann(1, "Anne");
      Student bobby(2, "Bobby");
  
      ann.parent = p_tim;
      bobby.parent = p_tom;
  
      std::map&lt;std::string, Student&gt; students;
      students.insert(std::make_pair("ann", ann));
      students.insert(std::make_pair("bob", bobby));
  
      for(auto student:students)
      {
          std::cout &lt;&lt; "Student: ID = " &lt;&lt; student.second.student_id &lt;&lt; " and Name: " &lt;&lt; student.second.student_name &lt;&lt; std::endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>I can view the contents of the map using the following command.</p>
  
  <pre><code>(gdb) p students
  $1 = std::map with 2 elements = {["ann"] = {student_id = 1,
      student_name = "Anne", parent = 0xa85ed0}, ["bob"] = {student_id = 2,
      student_name = "Bobby", parent = 0xa85f20}}
  </code></pre>
  
  <p>I want to inspect the <strong>parent</strong> object inside a single element in the map.
  For example, I want to inspect the phone number of ann's parent from this map. But I cant access it like below.</p>
  
  <pre><code>(gdb) p students["ann"].parent-&gt;phone_number
  Could not find operator[].
  </code></pre>
  
  <p>Can you please let me know how this is done in GDB?</p>
  
  <p>I think it should be possible(from command line GDB) because the visual debugger in VS Code shows this information. 
  <a href="https://i.stack.imgur.com/Vnsmj.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vnsmj.jpg" alt="enter image description here"></a></p>