5

So lets say we have a vector v and I pass this to a function f

void f(vector<int> &v) {
    cout<<&v;
}

int main() {
    vector<int> v;
    v.push_back(10);  
    v.push_back(20);
    v.push_back(30);

    cout<<&v[0]<<"\n"<<&v<<"\n";
    f(v);
}

For this I get the outputs as:

0x55c757b81300
0x7ffded6b8c70
0x7ffded6b8c70

We can see that &v and &v[0] have different address. So is v a pointer that points to the start of the vector? If so, why can't I access *v? Is there a mistake I'm making?

Thanks

  • Why do you want to access it this way? This is what iterators are for. – tadman Nov 23 '20 at 07:22
  • this is reference. their memory location. – foragerDev Nov 23 '20 at 07:23
  • `vector` is a template, `vector` is a class, and `v[0]` is an `int`. How, or even whether, the implementation uses pointers is an internal detail, not documented and not to be relied on. Why do you care? That the last two lines print the same address is expected and works the same for all reference type arguments. – dxiv Nov 23 '20 at 07:24
  • https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in check the difference between pointer and reference. – foragerDev Nov 23 '20 at 07:25
  • 1
    For what you can or can not do with `std::vector` types, consult the C++ standard, a good book or e.g. cppreference.com. If you are curious, you could also read the sourcecode for the `std::vector` template, though it's a bit hard to follow as a beginner. As a new user here, start with the [tour] and read [ask]. – Ulrich Eckhardt Nov 23 '20 at 07:27
  • 2
    `int *p = v.data(); cout<

    – Ch3steR Nov 23 '20 at 07:41

4 Answers4

11

Is vector in c++ a pointer?

No. std::vector is not a pointer. It is a class.

So is v a pointer that points to the start of the vector?

No, but v (an instance of the vector class) does happen to contain a pointer to the start of the array.

why can't I access *v?

Because there is no overload for the unary operator* for the class std::vector.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • So if I needed to access the address of any element in the middle is `&v[i]` my only method? – Rishab Balasubramanian Nov 23 '20 at 09:54
  • 1
    @RishabBalasubramanian You don't need to use the addressof operator to access the element. Furthermore, subscript operator is not the only way to access the elements. There are other ways such as using a pointer or a vector iterator. – eerorika Nov 23 '20 at 09:57
4

The std::vector structure may contain additional data, such as the length. The contiguous elements are stored in a separate location and that location changes as the array is resized. &v should remain the same so long as v isn't moved, but &v[0] can be invalidated for a number of reasons.

Imagine, in very rough terms, it's:

struct vector {
  size_t size;
  T* elements;
};

The operator[] implementation takes over when you employ &v[0].

It's not a pointer, and it behaves very differently from new[].

tadman
  • 208,517
  • 23
  • 234
  • 262
1

std::vector is a sequence container that encapsulates dynamic size arrays. So definately, it is not a pointer.

As @tadman said, v (an instance of the vector class) does happen to contain a pointer to the start of the array.

You cannot access *v because there is no overload for the unary operator* for the class std::vector.

Ranjeet R Patil
  • 453
  • 6
  • 10
0

No the vector in c++ is not a pointer. The vector is a class in c++