-1

In the cplusplus page for the inner_product module they gave a code example:

int init = 100;
int series1[] = {10,20,30};
int series2[] = {1,2,3};
std::cout << "using default inner_product: ";
std::cout << std::inner_product(series1,series1+3,series2,init);
std::cout << '\n';

Where I saw them use series1+3 when calling the inner_product funtcion.

What exactly does adding "3" to the array variable do?

user3330623
  • 111
  • 3

1 Answers1

4

What exactly does adding "3" to the array variable do?

The array operand decays into a pointer to first element of the array. 3 is added to that pointer, so that the result is a pointer to 3 elements after the first (i.e. the element at index 3), which is immediately outside the bounds of the array. The addition is equivalent to std::end(series1) which would be more idiomatic in my opinion.

I'm assuming that series1 on itself is a pointer to the first element of the array

You assume wrong. series1 is not a pointer, but an array. However, an id-expression (standardese for the name of the variable) of an array will decay into a pointer in value contexts.

eerorika
  • 181,943
  • 10
  • 144
  • 256