vector and pointers to a newbie
Too much time without C programming (15+ years) so I'm a totally newbie now.
My problem, sure it's simply, is that I'm storing pointers in a vector but they are modified.
I've a Point* point; which contains 4 points and I can access with point, point+1...
I've a vector <Point*> v, and I want to compare each Point* with the stored in the vector and under some conditions to store (a copy) of this point in the vector.
The code is something like
for( i = 0; i < total_points; i += 4 ) { //i+=4 because Point* is a pointer with 4 Points...
Point* rect =next point //each iteration I've checked I got the next and correct point
if(v.empty()) {
v.push_back(rect); //empty vector store the first rectangle
}
else{
check(rect,v); //ERROR inside function, rect and v[0] are the SAME
}
}
the problem that each time I call Point* rect =next point, rect changes BUT the point stored in v[0] changes TOO, because I stored the pointer to rect and it has changed, my silly question is, how can store in v pointers to Point* but they dont change in the next iteration when read a new value for rect.
Best regards.