Reputation: 159
Hi I'm new to c++ and wanted to ask a questions on how to access an element of struct inside a vector using pointers. lets say I have a struct:
struct pets{ //struct of pets
string name;
int age;
}
int main() {
pets a = {bolt, 2};
pets b = {crash, 3};
vector<pets> x;
x.push_back(a);
x.push_back(b);
vector<person> *ptr = &x;
???
}
using the pointer ptr to the vector x how would I be able to access the first the age of the the first element stored in my vector of pets? I am aware its easier to use
x[0].age
but I wanted to acces the elements member using a pointer to a vector of struct. Can anyone help?
Upvotes: 1
Views: 78