HOWTO · C++
How to Access std::vector Members Through a Pointer in C++
Learn how to call std::vector members through a pointer in C++, pass the vector pointer to a function, and iterate over its elements.
On this page
To call a member of a C++ vector through a pointer, use ptr->member; the equivalent form is (*ptr).member. The pointer must point to the vector object itself. Take that object’s address with &, then use either expression for members such as size() and at(). The example below also passes the pointer to a function and reads each value through it.
Use -> to Access a Vector Through a Pointer
Declare a pointer with the vector type followed by *. For a vector of integers, that type is std::vector<int>*. If values is an existing vector, &values is its address, so assigning it to the pointer makes the pointer refer to the vector. This does not copy the vector or create a separate collection.
The arrow operator combines dereferencing the pointer and selecting a member. Thus ptr->size() calls size() on the vector to which ptr points. The same pointer can be passed to a function whose parameter has a matching pointer type. In the complete example, report_size receives a pointer to a const vector because it only reads the vector. The const qualification prevents that function from modifying the vector through its parameter; it does not change how the pointer is passed.
Run this example:
#include <cstddef>
#include <iostream>
#include <vector>
void report_size(const std::vector<int>* ptr) {
std::cout << "Count: " << ptr->size() << '\n';
}
int main() {
std::vector<int> values{10, 20, 30};
const std::vector<int>* ptr = &values;
report_size(ptr);
for (std::size_t i = 0; i < ptr->size(); ++i) {
std::cout << (*ptr).at(i) << '\n';
}
}
Count: 3
10
20
30
The run prints the vector size followed by each value on its own line. The vector is an automatic local variable, and the pointer is used only while that vector remains in scope. In your own program, ensure a pointer does not outlive the object whose address it stores.
Use (*ptr).member to Access a Vector Through a Pointer
The expression (*ptr).member first dereferences ptr, producing the vector object, and then selects one of that object’s members with .. The parentheses matter: without them, the dot operator would bind to ptr before the dereference, which is not the intended operation. For a vector member, (*ptr).size() and ptr->size() mean the same thing. The arrow form is shorter; the dereference-and-dot form makes the two operations explicit.
In the example above, (*ptr).at(i) accesses an element by index after dereferencing the vector pointer. The member call is still made on the vector, while at(i) returns the selected integer. This distinction is useful when reading code: ptr has type pointer-to-vector, but (*ptr).at(i) refers to an element value. If a function needs to work with the vector, pass a pointer to the vector; if it needs one element, use an element reference or an appropriate element pointer instead.
Iterate Over Elements Through a Pointer to a Vector
The loop uses ptr->size() as its stopping condition and accesses each value with (*ptr).at(i). Both operations go through the pointer to the vector. You could write the indexed member call with the arrow form as ptr->at(i); it selects the same vector member. std::size_t, included from <cstddef>, is an unsigned type suitable for the vector’s size and indices.
A vector pointer is different from a vector iterator and from a pointer to an individual element. The pointer in this example refers to the whole std::vector<int> object. An iterator is a container-provided position used to traverse its elements, while an element pointer refers to one stored value. The indexed loop keeps those roles separate and is useful when an index is needed. If your task only requires visiting every value, a range-based loop over *ptr is also possible, but it iterates the vector rather than demonstrating member access through the pointer on every step.
Because the example does not change the vector while walking through it, its index-based access remains straightforward. In a program that inserts or removes elements during traversal, reconsider the loop: such operations can change the vector’s size and invalidate references, pointers, or iterators to its elements. A pointer to the vector object remains a pointer to that object, but it does not make element positions stable. Keep the vector alive for every use of its pointer, and choose iteration logic that accounts for any mutations your program performs.