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

This is a discussion on Access vector<structure> elements in a function, how? within the C++ Programming forums, part of the General Programming Boards category; Some help please. I have confused myself with pointers, structures and vectors again, and all references are only aggravating that ...

  1. #1
    Registered User KeithS's Avatar
    Join Date
    Jul 2009
    Location
    Colombia
    Posts
    21

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User KeithS's Avatar
    Join Date
    Jul 2009
    Location
    Colombia
    Posts
    21
    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. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User KeithS's Avatar
    Join Date
    Jul 2009
    Location
    Colombia
    Posts
    21
    Snap!

    Silly me. Thanks!!!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    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;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unable to access structure within a function
    By bluetxxth in forum C Programming
    Replies: 7
    Last Post: 02-17-2010, 03:52 AM
  2. Access a struct elements from...
    By Blackroot in forum C++ Programming
    Replies: 3
    Last Post: 08-26-2006, 06:00 AM
  3. Replies: 3
    Last Post: 03-22-2005, 03:27 AM
  4. Vector elements
    By strickey in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2005, 10:30 AM
  5. Pointer of arrays - how do I access their elements?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-12-2001, 12:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21