Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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: 420

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   Crossbones+   -  Reputation: 1318

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: 420

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