In C + + programming, we use arrays in our processes, this looks simple because an array is a collection of elements of the same type, but when you haven't considered the address of an array, everything is simple, and if you touch the address concept in an array, you may change your mind.
Below, I'll list a few examples of what these examples output will look:
First give the definition of the array: int a[10];
1. cout<<a<<endl;
2. cout<<&a<<endl;
3. Cout<<a +1<<endl;
4. Cout<<&a +1<<endl;
Is it a little confusing?
Okay, now, I'm going to one by one to explain these things:
1.cout<<a<<endl;
When we define an array, we give the array a name, called "a", then a refers to the piece of memory that the system assigns to us (40 bytes in this case), so naturally, a represents the starting address of the block of memory.
2. cout<<&a<<endl;
Obviously, this is a means of taking an address to a and still getting the starting address of that block of memory.
3. Cout<<a + 1<<endl;
At this point, we add 1 to the starting address, to determine what the result is, we need to know what type of data a can store, for this example, the integer data stored in a, so it refers to the address value of the second element in a, and the result value of 4 bytes for the starting address plus
4. Cout<<&a + 1<<endl;
This is probably the hardest word to understand, before we get the results, we need to see what a represents, and a represents a memory that can store 10 integer variables, so add 1 to the &a to the next memory that can store 10 integer variables.
Below, give a screenshot of the Code and Effect of the test:
#include <iostream>
using namespace std;
int main (void)
{
int a[10];
cout<<a<<endl;
cout<<&a<<endl;
Cout<<a + 1<<endl;
Cout<<&a + 1<<endl;
System ("pause");
return 0;
}
See if the results are the same as I said.