Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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...
}
[993 byte] By [vanderpj123] at [2007-11-11 7:58:19]
# 1 Re: Passing Vector to another function for modification
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 at 2007-11-11 21:01:46 >
# 2 Re: Passing Vector to another function for modification
My Bad, I figured it out. Sometimes I wonder what goes on inside my head...
vanderpj123 at 2007-11-11 21:02:47 >