How do I create an array of pointers?


Question

I am trying to create an array of pointers. These pointers will point to a Student object that I created. How do I do it? What I have now is:

Student * db = new Student[5];

But each element in that array is the student object, not a pointer to the student object. Thanks.

1
37
8/5/2009 8:53:00 AM

Accepted Answer

Student** db = new Student*[5];
// To allocate it statically:
Student* db[5];
78
3/6/2009 11:31:13 PM

#include <vector>
std::vector <Student *> db(5);
// in use
db[2] = & someStudent;

The advantage of this is that you don't have to worry about deleting the allocated storage - the vector does it for you.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon