Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:40:07+00:00 2026-05-26T21:40:07+00:00

If I have the following: vector<int> v(4,0); vector<int>* p = &v; int element =

  • 0

If I have the following:

vector<int> v(4,0);
vector<int>* p = &v;
int element = p[0];

Will element be the same value as v[0]? I’m getting confused here about the [] operator.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:40:08+00:00Added an answer on May 26, 2026 at 9:40 pm

    Short answer: no.

    What you have is not even valid C++. p[0] is the same as *p, which is of type vector<int> and thus not convertible to int.

    The []-notation merely suggests that you think of p as an array of vectors, and you are accessing the array’s first element, which is v. More generally, for any pointer p, the notation p[k] is identical to *(p + k) (in fact to a fault, as you can say a[5] and 5[a] interchangeably).

    So if you really wanted to, you could write p[0][i] for the ith element of the vector, though it is more customary to just write (*p)[i] (parentheses needed for the correct precedence).


    When I first skimmed over the question, I thought you might be looking for some clever hack and wanted to know whether **(int**)(p) was equal to v[0]. That is indeed plausible, as the first element of the vector’s data structure is often the pointer to the vector’s internal buffer. Don’t use this at home.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following template declaration: template <typename T> void IterTable(int& rIdx, std::vector<double>& rVarVector,
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have the following in my Puzzle.h class Puzzle { private: vector<int> puzzle; public:
I have the following code #include <algorithm> #include <iostream> #include <vector> #include <functional> int
I have the following code: #include <iostream> #include <vector> #include <algorithm> int main( int
I have the following code: int MimeDocument::GetAttachmentId( std::string const& content_id ) { using namespace
Suppose I have a class similar to the following: #include <vector> class element {
I have the following class: Class L{ public: bool foo(vector<bool> & data); private: C**
IE what happens if you have this following piece of code? int mean(const vector<int>
I have the following code: void foo() { vector<double> v(100,1); // line 1 //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.