Sign in to follow this  
Lith

C++ Pointer to a Vector Syntax

Recommended Posts

Lith    429

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

Share this post


Link to post
Share on other sites
Lith    429

*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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this