Lucas Steffen
Lucas Steffen

Reputation: 1364

Safest way to access the last address of an array C++

Imagine these:

int main (void)
{
    int V[101];
    populateSomehow(V);
    std::sort(V, &V[100]); //which one
    std::sort(V, V+100);
}

Is there a 'safer one'?

Upvotes: 0

Views: 164

Answers (2)

Krypton
Krypton

Reputation: 3325

There is another way in C style:

int V[101];

std::sort(V, V + sizeof(V)/sizeof(V[0]));

Upvotes: 1

taocp
taocp

Reputation: 23654

You can use std::begin and std::end since c++ 11. For example:

int V[100];

std::sort(std::begin(V), std::end(V));

Upvotes: 4

Related Questions