-1

In many posts I have seen answers like

std::sort(arr1, arr1 + size1);

I do not understand what is happening at arr1 + size1 where an array is being added with an integer, is it concatenation, or is it adding size1 to each element of the array? Neither of them seem like a logical thing required to sort an array. I tried printing the result but, it isn't possible to print arrays in C++. Could someone please explain what is going on here?

2 Answers2

1

It's pointer arithmetic. arr1 is a pointer to the beginning of the array, and arr1 + size1 points to just beyond the end of the array.

jjramsey
  • 1,131
  • 7
  • 17
  • 1
    More explicitly, it's a pointer to the **first element** of the array. That's important because the type of the pointer is pointer-to-element, not pointer-to-array-of-elements. +1. – Pete Becker Sep 05 '19 at 19:17
0

It's adding the integer to the address of the array (the address of the first element). Yielding an address (if the arithmetic is correct) to an element in the array.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70