# Vector as array

Discussion in 'C++' started by Alex Vinokur, Sep 21, 2008.

1. ### Alex VinokurGuest

Let v be an vector; for instance:
std::vector<double> v;

Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?

In other words,
Let foo() have the following prototype:
void (double* p, std::size_t arrarySize);

Is this calling is correct:
foo (&v[0], v.size()) ?

Alex Vinokur

Alex Vinokur, Sep 21, 2008

2. ### Kai-Uwe BuxGuest

Alex Vinokur wrote:

> Let v be an vector; for instance:
> std::vector<double> v;
>
> Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?
>
> In other words,
> Let foo() have the following prototype:
> void (double* p, std::size_t arrarySize);
>
> Is this calling is correct:
> foo (&v[0], v.size()) ?

I think that is what the standard means by calling the memory of a
vector "contiguous". The precise guarantee is for types other that bool
[23.2.4/1]:

... The elements of a vector are stored contiguously, meaning that if v is
a vector<T, Allocator> where T is some type other than bool, then it obeys
the identity &v[n] == &v[0] + n for all 0 <= n < v.size().

Best

Kai-Uwe Bux

Kai-Uwe Bux, Sep 21, 2008

3. ### Rolf MagnusGuest

Alex Vinokur wrote:

> Let v be an vector; for instance:
> std::vector<double> v;
>
> Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?

Yes.

> In other words,
> Let foo() have the following prototype:
> void (double* p, std::size_t arrarySize);
>
> Is this calling is correct:
> foo (&v[0], v.size()) ?

Yes.

Rolf Magnus, Sep 21, 2008