Enoch Thomas
Enoch Thomas

Reputation: 35

returning a vector pointer from a function

Hi I am trying to understand algorithms But i just don't understand using them in functions. In this case passing a vector into a function. The main issue in understanding is with the return part of the " string* ptrToElement(vector<string>* const pVec, int i)". I dont know why in this function when returning, "return (&( (*pVec)[i] ))", why is does pvec have a *. Why is it a pointer. This is what baffles my understanding, I sont get why pvec has

string* ptrToElement(vector<string>* const pVec, int i);
int main()
{

vector<string> vecInventory;

vecInventory.push_back("sword");
vecInventory.push_back("shield");
vecInventory.push_back("armour");

cout << "Sending the object pointed to by returned pointer: " << endl;
cout << *( ptrToElement(&vecInventory,0) ) << endl << endl;


int iTemp = 0;
cin >> iTemp;
return (0);
}



string* ptrToElement(vector<string>* const pVec, int i)
    {
    return (&( (*pVec)[i] ));
    }

Upvotes: 0

Views: 921

Answers (3)

Jerry Jeremiah
Jerry Jeremiah

Reputation: 9643

To understand this you just need to visualize what the variables and pointers actually mean in terms of memory locations.

The compiler and program loader will pick real memory addresses and those locations will probably change every time the program is run so we will just make up some values for demonstration purposes.

vecInventory is a vector object and it takes up some memory because it has some data inside it so let's just say for demonstration purposes that it is stored in memory starting at address 100. If you write vecInventory in the program it refers to the vector object.

&vecInventory is the address of the vector object. So &vecInventory is just the number 100 because that is the memory location of the vector object.

So now when you pass &vecInventory to ptrToElement, that function receives the value 100 which it stores in pVec. So pVec contains the number 100.

*pVec means the thing stored at the location stored in the pointer. pVec contains the value 100 which is the memory location of the vector object so *pVec is the vector object.

You could have just passed the vecInventory vector object to ptrToElement directly instead of passing the memory location of the vector object but passing values to a function means copying the value passed. So if you pass a number (like a memory location) only a single number is copied whereas passing an object directly could be a problem if that object is really big because copying something really big could take a while. In this case it doesn't matter because a vector is small (the data you put into a vector is not stored in the vector object).

(*pVec)[i] is the value stored at entry i of the vector. Remember I said the data you put into a vector isn't stored in the vector object? So when i=0 in your example let's just say for demonstration purposes that the value at index 0 of the vector is stored in memory starting at address 50.

&(*pVec)[i] when i=0 is the number 50 because that is the location where the value at index 0 is stored in memory. That means the value returned from ptrToElement(&vecInventory,0) is the number 50.

*( ptrToElement(&vecInventory,0) ) means the thing stored at the location stored in the pointer. ptrToElement(&vecInventory,0) returns the value 50 which is the memory location of the value stored at index 0 so *( ptrToElement(&vecInventory,0) ) is the value stored at index 0 of the vector object.

So in our example demonstration:

  • vecInventory is the vector object (which is stored at memory location 100)
  • &vecInventory is the memory location 100
  • pVec is the memory location 100
  • *pVec is the vector object (which is stored at memory location 100)
  • (*pVec)[i] when i=0 is the value at index 0 of the vector (which is stored at memory location 50)
  • &(*pVec)[i] is the memory location 50
  • ptrToElement(&vecInventory,0) is the memory location 50
  • *( ptrToElement(&vecInventory,0) ) is is the value at index 0 of the vector (which is stored at memory location 50)

Upvotes: 2

2power10
2power10

Reputation: 1279

Fei Xiang have given a good explanation. I would like to add an explaination about the *.

In your code, there are two different meanings of *, I think that might be one of the reasons you don't understand it well.

  • vector<string>*, this mean a pointer to a vector object. * is part of the type.
  • (*pVec). * here is dereference the pointer into its pointee. The purpose of dereference here is to use vector's operator [] to access the vector element. You could use pointer to do that directly, code looks like pVec->operator[](i).

Upvotes: 2

eesiraed
eesiraed

Reputation: 4654

As Igor Tandetnik noted, the code doesn't make a lot of sense.

pVec is a pointer to a vector of strings, and *pVec gives the vector it points to. Then, the [i] gives the string indexed i inside a vector, and finally, the & gives a pointer to the string. So this function returns a pointer to the element indexed i inside a vector of strings pointed to by pVec.

Upvotes: 1

Related Questions