Pointers?

I'm somewhat new to pointers, when I ran this program, I got an output where I'm confused. How do you get those long addresses for some of those? How would you know this if I didn't run this on compiler?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <iostream>
using namespace std;

int main() {


	int x = 2;
	int *ptrX = &x;

	int arrayX[3] = { 4, 6, 8 };
	int *ptrArrayX = arrayX;

	cout << "a:" << &x << endl;
	cout << "b:" << x << endl;

	cout << "c:" << &ptrX << endl;
	cout << "d:" << ptrX << endl;
	cout << "e:" << *ptrX << endl;

	cout << "f:" << &arrayX << endl;
	cout << "g:" << arrayX << endl;
	cout << "h:" << *arrayX << endl;

	cout << "i:" << &ptrArrayX << endl;
	cout << "j:" << ptrArrayX << endl;
	cout << "k:" << *ptrArrayX << endl;


	system("pause");
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
a:0x7cd64ee2894c
b:2
c:0x7cd64ee28950
d:0x7cd64ee2894c
e:2
f:0x7cd64ee28960
g:0x7cd64ee28960
h:4
i:0x7cd64ee28958
j:0x7cd64ee28960
k:4


How do you it's 0*7 and where do you get the other stuff after that??
Last edited on
How do you get those long addresses for some of those?

Those long numbers represent the memory locations that the values have been placed into.

How would you know this if I didn't run this on compiler?

There is no way to know what they are going to be just by looking at the source code; their value depends on where in the actual memory those values get placed, which is a combination effect of the compiler and the operating system.
So if this was on an exam and I couldn't use a compiler, what would I put? and
Would I know that it is 0*7?
0x just means the number being shown to you is in hexadecimal format. It's just a number. 0x7cd64ee28960 is the decimal number 137259888314720.

If an exam showed you this code, and asked you what memory address variables would be in, and you hadn't already been told a whole lot of detail about how the compiler etc. likes to organise its memory, you would write "Are you serious? Am I paying you actual money for this education? You don't know what you're talking about and I'm very angry about it."

https://stackoverflow.com/questions/18446171/how-do-compilers-assign-memory-addresses-to-variables
Oh ok thanks, I think I sort of understand it now.
So how would say a professor possibly put it: Would he just say 0*7 or so? I'm a noob, I'm still learning slowly...
Last edited on
Edit: I have been double ninja'd :+) Have to learn to type faster :+D

How do you get those long addresses for some of those?


You don't. They are just whatever the address of that variable happens to be. The address is originally 64 bit (or 32 bit) 0's and 1's, it's displayed in hex format with the leading 0x.

What that number actually is, is irrelevant: The address of variable X is what it is, we don't care what it's actual number is. This is true even when we do pointer arithmetic. If one was to add 1 to the variable which holds the address of the first element in an array, the compiler uses the size of the type of the array to calculate the address of the next element in the array.

How would you know this if I didn't run this on compiler?


The addresses will vary, quite possibly every time you run it on the same machine, and definitely with different machines / compilers. But as I say, it's irrelevant.
Last edited on
'x'. It's a 'x'. Not a '*'.

It's how we express that a number is in hexadecimal (base 16).

0x10 (hexadecimal - base 16)

is the same as

16 (decimal - base 10 - this is the base you're most familiar with)

which is the same as

b 10000 (binary - base 2)

which is the same as

020 (octal - base 8)


It's just a convention to expresses memory address in base 16 numbers (hexadecimal). It's just a number.
Ok thanks for the help, appreciate it!
Topic archived. No new replies allowed.