Question about arrays

I am reviewing some C++ code and ran across the following line of code:

class->method(label, array + integerValue)

the code in question is: array + integerValue

array is declared as: float array[850];
integerValue is declared as: int integerValue;

integerValue can be either 0 or 25.

So the question is what is array + integerValue actually doing?
Last edited on
The type of the expression is float *

So array + integerValue provided that integerValue is equal to 25 means

&array[25] that is equivalent to array + 25
Last edited on
closed account (o3hC5Di1)
Hi there,

Let's say integerValue has value 25.
In that case what the code does is to pass the address of the 26th member of the array.

It works like this:

array is an array of floats, the single name array (without brackets) will hold the address in memory of the very first member of the array - array[0].
When we do arithmetic on that address, the compiler is smart enough to interpret that as such:

array (start address) + 25 times the size of a float (because the array consists of float values).

That lands you at the 26th member of the array member [code]array[25].
More info here: http://cplusplus.com/doc/tutorial/arrays/#accessing_values

Hope that's a bit clear, let us know if you need further clarification.

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.