Passing a vector between functions via pointers

I was asked to use pointers to add a vector that I would like to pass from some existing function to another function. I really got stuck on how to get the information back from this pointer. I tried a number of things that I read here and there, so let me demonstrate what I'm talking about.

main program:

std::vector<float> * dvertex=NULL; track.calculate(irrelevant stuff, dvertex) 

secondary program (track, calculation)

 track::caclulate(irrelevant stuff, vector<float> * dvertex) { ... vector<float> pos; ... pos filled after some calculations if(! (dvertex==NULL)) { dvertex = &pos1; } 

go back to the core, if I didn’t mix something up above, here are some things I tried

one

 (*dvertex).at(0) float z = (*dvertex).at(0) 

2

 (*dvertex)[0] 

and a bunch of things that just didn't compile. I'm pretty stuck as I'm not sure how to get specific values ​​from this vector in the main program. I even thought that it could be the if (! (Dvertex == NULL)) bit, so I changed it to if (dvertex == NULL), but still is not happy. Any help would be greatly appreciated.

* Change / Update * Thanks a lot to everyone for their help, but I'm afraid that everything is still wrong.

So, following the recommendations that I just passed the link: I did this:

primary

 std::vector<float> dvertex; track.calculate( foo, &dvertex); 

secondary remained unchanged (with verification!)

primary

 std::cout<<dvertex[0]<<std:endl; 

(among other attempts to actually use the data)

Thanks so much for any thoughts on what I'm still doing wrong. Everything compiles, the program simply freezes when it reaches the point at which data from dvertex is used.

Edit: final fix

in the secondary program that I needed

 *dvertex = pos1; 

instead

 dvertex = &pos1; 
+6
c ++ pointers vector
source share
3 answers

I am not sure why they did not compile for you, because they are valid if the pointer is valid and not null:

 void f(std::vector<int>* v) { if( v != 0 ) { int n = (*v)[0]; // ok int m = (*v).at(0); // ok int o = v->at(0); // ok } } 

But it doesn’t matter. Use a link if you must change the vector and a link to const if you should not. Rarely, if ever you need to take a container with a pointer.

In addition, I suggest you check pointers to 0 , not NULL , because sometimes NULL is defined as (void*)0 according to the C compilers. But some people can argue differently.

+10
source share

If you are going to change the vector, you probably just want to pass it by reference. However, if you use a pointer, you need to define the vector in the main, and then pass the address of this vector:

 void calculate(std::vector<float> *vertex_array) { vertex_array->pushback(1.0f); vertex_array->pushback(2.0f); } int main() { std::vector<float> vertexes; calculate(&vertexes); std::copy(vertexes.begin(), vertexes.end(), std::ostream_iterator<float>(std::cout, "\n")); return 0; } 
+3
source share

See my note above, but for your script to work, you need std::vector<float> * dvertex=NULL; be std::vector<float> * dvertex = new std::vector<float>();

+2
source share

All Articles