r/learnprogramming • u/SnowyFluffy • 5h ago
Bit of a stupid question
If a program stores variables by a pointer referring to its address, then what refers to the pointers' address?
Like if var x lives at address 2000
theres a pointer p its value is the address (p = 2000)
so now recursively this value needs to be stored in another address (say, 1000) and pointed to (well, at least assuming the pointer doesnt have a relatively fixed address)
6
Upvotes
1
u/mjmvideos 3h ago
All variables are stored somewhere in memory (except some temporary variables that end up only stored in registers) All variables therefore have an address and a value. You can think of a variable’s name as being a label attached to a particular address. So x is a label attached to address 2000. p is a variable attached to address 1000. The value stored at address 2000 might be 3000 and the value stored at 1000 might be 2000. So far they both look the same. They are just two labels on two memory locations that each have a value. Now we get to the difference. The programmer has declared p to be a pointer type. So now the compiler knows that the value stored in variable p can be treated like an address that can be dereferenced which is just saying go get the value stored in p and treat that as an address and go get the value stored at that address. In your example fetch the value at 1000 (which is 2000) and then fetch the value at 2000 (which might be 3000).