arrays and addresses

According to my logic the output should be 2, since p is the third member of given array. However, it isn't. Why?

1
2
3
4
5
6
7
8
9
 #include <iostream>
using namespace std;
int main()
{
    int p, x[]={1,2,p,4,5,6};
   cout << &p;
}

Last edited on
What output are you getting?

Looks like you're printing the address of p, not any value. Plus you never actually use x for anything. But if you tried to print x[2] you would probably get garbage since p isn't initialized.
You're using an uninitialized value. The value of p is undefined at the point when you put it into the array.

Also, when you print out &p, you are asking for the address where p lives, not the value stored in p. Nor, for that matter, are you asking about the contents of the array. (If x were changed, p wouldn't reflect those changes -- p was just used to fill the array in the first place, it has no further involvement in x.)

The address of an array element is not the same thing as its index. (You may find it interesting to print out the addresses of each successive element, and notice the pattern, and compare it to sizeof int).
Last edited on
Thank you, I thought that the address where p lives is "two", since it's third member of array ( I thought that array cells act like memory cells..)
Last edited on
The array has nothing to do with p. p has it's own address and value.

Thank you, I thought that the address where p lives is "two", since it's third member of array


It's actually going to be: the address of the start of the array, plus (the index times the size of each element).

Edit: I mean, the address of the array element you're talking about. The address of p itself is unrelated to any of the addresses in the array.
Last edited on
So if I wanted to check the address of array element, how do I do that?
Last edited on
Print the address of the element

cout << &array[2] << endl;
Therefore, the two outputs of the following code will be different:

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main()
{
    int p; int x[]={1,2,p,4,5};
    cout << &x[2] << endl;
    cout << &p << endl;

}


?
Yes. (try it!)

Try running this program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
	int p = 30;
	int garbage[500];	// Ignore this.

	int arr[] = {10, 20, p, 40, 50}; // Initialize an array, copying the value of p.
	
	cout << "An int is: " << sizeof(int) << " bytes on this environment." << endl;
	cout << "&p == " << &p << endl; // Print the address of p.

	for(int i = 0; i < 5; i++)	// Print the address of each array element.
		cout << "&arr[" << i << "] == " << &arr[i] << endl;

	cin.get();
}


When you look at the result, notice that: 1) p doesn't live anywhere in the array, and 2) the array elements' addresses are evenly spaced. If an int takes 4 bytes, then the first 4 bytes of the array are the first int, and the next 4 bytes are the next, etc. etc.
Thank you very much :)
Topic archived. No new replies allowed.