Mohamed Iqzas
Mohamed Iqzas

Reputation: 1046

getting pointer to a vector C++

this is my second basic question on pointers. I am calling a function exposed in DLL..

A vector is being declared and populated with values inside that function being called.

I need to loop through the vector and access its values from the calling function.

int calling_function()
{
int* vectorSize;
string input = "someValue";
vector<customObjects> *v;// do i need a pointer to a vector here?

void function_being_called(input,v,&vectorSize);

//need to access the vector here...

}

void function_being_called(string input, void *returnValue, int* vectorSize)
{
vector<customObjects> v;
v.push_back(myObj);

*vectorSize= v.size();

*returnValue = ? // how to pass vector to the calling function through this parameter pointer variable

return;
}

Upvotes: 0

Views: 81

Answers (2)

Sean
Sean

Reputation: 62472

You've got two options. First, pass the vector as a reference:

string input = "someValue";
vector<customObjects> v;
function_being_called(input, v);

void function_being_called(string input, vector<customObjects> &v)
{
 // Whatever
}

Or, if you're using C++11 just return a vector and let the move constructor take care of it:

string input = "someValue";
vector<customObjects> v =  function_being_called(input);

vector<customObjects> function_being_called(string input)
{
  vector<customObjects> v;

 // Whatever

  return v;
}

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249093

It should be like this:

int calling_function()
{
  string input = "someValue";
  vector<customObjects> v;

  function_being_called(input,&v);

  // access the vector here...

}

void function_being_called(string input, vector<customObjects>* v)
{
  v->push_back(myObj);
}

Upvotes: 2

Related Questions