Wednesday 19 October 2016

Printing value of pointer in a vector of pointer c++

here is my code:



vector *ptr;
int *tab = new int(20);
ptr->push_back(tab);
cout << *(ptr->at(0)) << endl;



I want to print 20 on the screen, but I got a segmentation fault.
when I use only



vector ptr;


it prints out fine. I get easily the result just by doing :




*ptr.at(0);


But I want to use a pointer not a simple variable.
Can I have some enlightenment?



Thanks

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...