Question about Array values.

Hello everyone, I have some questions about the difference between the value of an array, and the array itself.

eg:

1
2
3
4
5
6
7
//This code will assign the value of [element 1] + 1 to the variable "value1".
const int size = 3;
int array[size] = {1,2,3};
int value1;

value1 = array[1] + 1; //thus, the value that value1 holds is 3.
    


but what about:

1
2
3
4
5
6
7


const int size = 3;
int array[size] = {1,2,3};

array + 1;


So, what does it mean if I add a value of 1 to "array"?

thanks
Last edited on
Nothing. That result is just discarded since you aren't assigning it to anything.

'array' is really just a pointer. You can add to a pointer, and you can access another element in the array by using an offset, but you don't assign 'array + 1' to anything, so the temporary result is thrown away.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
	int arr[3] = { 1, 2, 3 };
	int value1;

	value1 = arr[3] + 1; //value1 = 4

	for (int i = 0; i < 3; i++)
	{
		std::cout << "arr + " << i << ": " << arr + i << std::endl;
                std::cout << "arr[" << i << "] = " << *(arr + i) << std::endl;
	}

	std::cout << "Values remain unchanged" << std::endl;
	for (int i = 0; i < 3; i++)
		std::cout << "arr[" << i << "] = " << arr[i] << std::endl;

	return 0;
}


produces

arr + 0: 001FFE4C
arr[0] = 1
arr + 1: 001FFE50
arr[1] = 2
arr + 2: 001FFE54
arr[2] = 3
Values remain unchanged
arr[0] = 1
arr[1] = 2
arr[2] = 3
Press any key to continue . . .
Last edited on
ohh i see, I got it now.
I was writing a program with array, and I mistakenly wrote array + 1 instead of array[value] + 1, and my program outputted some random values, then I got curious of this.

thank you very much for the clarification.
Topic archived. No new replies allowed.