# Thread: Access vector<structure> elements in a function, how?

1. ## Access vector<structure> elements in a function, how?

Some help please. I have confused myself with pointers, structures and vectors again, and all references are only aggravating that confusion at this stage. I need a clear, specific answer, please...

I'll start here, in case I am totally wrong right from the very beginning. I make a structure globally, like this;

Code:
```// Globals
struct a_struct
{
double x_position;
double y_position;
};```
Then in main I declare it as a vector;

Code:
```int main()
{
vector<a_struct> position;

//......
}```
Here I have had no trouble, as I can pass it to a function to "push_back" the vector with success, like this;

Code:
```// The Function
void increase_vec_size(vector<a_struct> *posit)
{
a_struct temp_struct = {0};
temp_struct.x_position = 10.5; // for example..
temp_struct.y_position = 15.2;
posit->push_back(temp_struct);
}

// And it was called from within main() like this...
int main()
{
//.......

increase_vec_size(&position);

//......
}```
So, that works. But now I want to access the conents of my vector in another function. I run into trouble...

Code:
```// The Function
void access_vec_elements(vector<a_struct> *posit, size_t vec_sz)
{
double x_pos;
double y_pos;

// This does not work...
x_pos = posit.at(vec_sz).x_position;
y_pos = posit.at(vec_sz).y_position;

// Nor does this...
x_pos = posit[vec_sz].x_position;
y_pos = posit[vec_sz].y_position;

// And I certainly am not able to do this...
x_pos = posit->at(vec_sz)->x_position;
y_pos = posit->at(vec_sz)->y_position;
}

// And it is called from main() this way...
int main()
{
//.......

access_vec_elements(&position, position.size());

//.......
}```
So, how do I access the elements of a vector structure in a function?

I can't stand stabbing around randomly trying to figure it out. I honestly can't get it, so I ask for some help. Sorry if it looks really basic.

My sincere thanks in advance for any assistance.

2. Honestly, it would be a lot clearer and simpler if you just passed a reference to your vector, rather than a pointer.

All the things in access_vec_elements() either access something which doesn't exist, or access something off the end of the vector.

3. Thanks, Salem, but you see that's just it. I am having trouble figuring out how to pass a reference of something that already appears to have pointer behaviour. I would love to treat it as an if passing array, but it does not like it one little bit, at least the way I have tried (which is wrong).

The best I can do is something in between all the attempts above, like such;

Code:
```void access_vec_elements(vector<a_struct> *posit, size_t vec_sz)
{
double x_pos;
double y_pos;

x_pos = posit->at(vec_sz).x_position;
y_pos = posit->at(vec_sz).y_position;
}```
However, I am not at all sure that this is a very good way to do it, as my understanding of vectors is clearly deficient.

My thanks, just the same.

4. Like I said, vec_sz is off the end of the vector.

Try
x_pos = posit->at(0).x_position;
y_pos = posit->at(0).y_position;
to access the first element of the vector.

5. Snap!

Silly me. Thanks!!!

6. Code:
```void access_vec_elements(vector<a_struct> & posit) // Passing by reference
{
size_t vec_sz = posit.size(); // No need to send along vector's size. It keeps track of that itself.

// NO CONFUSION! YAY!
double x_pos = posit.at(vec_sz - 1).x_position;
double y_pos = posit.at(vec_sz - 1).y_position;
}```