6 Replies - 421 Views - Last Post: 31 July 2014 - 08:43 AM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Return a pointer to the vector

Posted 30 July 2014 - 10:51 AM

Ok here is the code

//assuming that all the directives have been included here

void ptrPointer(vector<string>* const p, int n);

int main()
{
  vector<string> inventory;
  vector.push_back("sword");
  vector.push_back("knife");
  vector.push_back("dagger");
  ptrPointer(&inventory, 0);
}

void ptrPointer(vector<string>* const p, int n)
{
  cout << &((*p)[n])); //display the address. It works
  cout << *(p[n]); //display the value of the variable to which the pointer points. It works
  //cout << p[n]; // error...won't display the address
}



Well...my question is how come the last statement or cout statement to be accurate won't work and display? Since we can do something like this below?

string text = "some text";
string *p = &text;
cout << p;



I passed the address of the vector to the function. And then display the address. It is a pretty common operation. But it just won't work. But then we do something simple similar to that of the vector. But it works.

This post has been edited by kenryuakuma: 30 July 2014 - 04:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Return a pointer to the vector

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2144
  • View blog
  • Posts: 3,295
  • Joined: 21-June 11

Re: Return a pointer to the vector

Posted 30 July 2014 - 12:03 PM

p is pointing to a single vector. p[n] tries to access the nth vector in the array of vectors that p points to, but since p only points to a single vector that will be an out of bounds access for n > 0. More importantly (as far as the compiler is concerned anyway) p[n] has type vector<string> and vectors can't be printed, so the line won't compile.

PS: If *(p[n]) works (which I doubt very much for any n other than 0), it works merely by accident. As I said p[n] is out of bounds for any n other than 0 and therefore invokes undefined behavior.
Was This Post Helpful? 0
  • +
  • -

#3 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Return a pointer to the vector

Posted 30 July 2014 - 02:08 PM

Thanks for you help sepp2k. But I thought the syntax of acessing the vector element is like this p[n], same as the array syntax. That's why I am just wondering why it won't work.

Besides, according to your explanation, with the syntax like this p[n], it means it access another vector other than the initial vector? I am just confused.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4220
  • View blog
  • Posts: 13,229
  • Joined: 25-December 09

Re: Return a pointer to the vector

Posted 30 July 2014 - 03:12 PM

My first question is why are you passing that vector by pointer notation instead of by reference?

Also you're snippet shouldn't compile since the function prototype is different from the actual function implementation.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Return a pointer to the vector

Posted 30 July 2014 - 04:54 PM

Ok it was just a typo...I have edited it. The problem is that I am learning how the function could return the a pointer, either returning by reference or by value. However, I am still puzzled by this problem and hope some of you could help and explain the reason.

since we can access the vector with the array index but how come I cannot do that through the pointer, which is used as a parameter of the function.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2144
  • View blog
  • Posts: 3,295
  • Joined: 21-June 11

Re: Return a pointer to the vector

Posted 30 July 2014 - 05:50 PM

View Postkenryuakuma, on 30 July 2014 - 11:08 PM, said:

Thanks for you help sepp2k. But I thought the syntax of acessing the vector element is like this p[n], same as the array syntax.


If p were a vector, then p[n] would indeed access that vector. But it's not. It's a pointer, so you're using [] on a pointer, not on a vector. If you want to index into the pointed-to vector, you need to dereference the pointer and then use [] on that.

Quote

Besides, according to your explanation, with the syntax like this p[n], it means it access another vector other than the initial vector?


When n is != 0, yes. But as I said, that would actually be an out-of-bounds access, so it'd invoke undefined behavior.
Was This Post Helpful? 1
  • +
  • -

#7 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Return a pointer to the vector

Posted 31 July 2014 - 08:43 AM

So I see...again thanks for your reply sepp2k. Now the concept seems to be clear.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1