Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ Pointer to a Vector Syntax


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Lith   Members   -  Reputation: 325

Like
0Likes
Like

Posted 22 May 2014 - 01:59 PM

Quick question, if I declare this pointer to a vector:

std::vector<int>* myVector = &someOtherVector;

Why does this not work?

*myVector[0] = 42;

And why does this work?

(*myVector)[0] = 42;

Thanks.


Edited by Lith, 22 May 2014 - 02:01 PM.


Sponsor:

#2 Jan2go   Members   -  Reputation: 559

Like
13Likes
Like

Posted 22 May 2014 - 02:10 PM

*myVector[0] = 42; evaluates to *(myVector[0]) = 42; because operator[] has a higher priority than operator*(). As a result, you're trying to dereference an integer which causes the error.



#3 Lith   Members   -  Reputation: 325

Like
0Likes
Like

Posted 22 May 2014 - 02:32 PM

*myVector[0] = 42; evaluates to *(myVector[0]) = 42; because operator[] has a higher priority than operator*(). As a result, you're trying to dereference an integer which causes the error.

 

All clear now! Thank you very much.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS