Passing Vector to another function for modification


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Passing Vector to another function for modification

  1. #1
    Join Date
    Aug 2004
    Posts
    5

    Passing Vector to another function for modification

    What I am attempting to do, is basically load a vector from another function. The code below is what I have attempted, but it won't work. In short, I declare a vector that will contain pointers to a struct. I pass the address of the vector to another function that will create a number of structs using malloc and store the pointers to those memory locations in the Vector. It doesn't seem to be working, can anyone help????

    typedef my_struct* struct_ptr;
    typedef struct my_struct{
    double value;
    }MY_STRUCT;

    void loadvector(vector<struct_ptr>* structPtr_vector){
    for(int j = 0; j < 10; j++){
    struct_ptr thePtr = (struct_ptr)malloc(sizeof(MY_STRUCT));
    structPtr_vector.push_back(thePtr);
    }
    }


    void function_1(){
    vector<struct_ptr> structPtr_vector;
    function_2(&structPtr_vector);
    printfVector(structPtr_vector); //code not here I got this...
    }
    vanderpj123

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Fosr starters, you need to call push_back like this:

    structPtr_vector->push_back(thePtr);

    Secondly, what are the prototypes of
    function_2 & printfVector?
    what are the error messages you're getting, if any?
    Finally, malloc doesn't create a real object. It just returns a chunk of raw memory so you can't expect the loadVector function to fill the vector with meaningful data.

    secondly,
    Danny Kalev

  3. #3
    Join Date
    Aug 2004
    Posts
    5
    My Bad, I figured it out. Sometimes I wonder what goes on inside my head...
    vanderpj123

Similar Threads

  1. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 10:09 AM
  2. Packed Data(Comp-3, etc)
    By Marcos in forum VB Classic
    Replies: 3
    Last Post: 01-25-2006, 12:18 PM
  3. Getting a list of files into an array
    By Scott in forum VB Classic
    Replies: 12
    Last Post: 12-21-2001, 05:21 PM
  4. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 07:53 AM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center