I just need some clarification on variables
A normal variable has 2 “parts” to it?
one part is the actual value and the other part is the location of that value in the memory
Is that right?
So a pointer variable is just the location part of a normal variable, and it doesn’t have value itself?
If you’re talking about C, then pointers simply represent another level of indirection.
If you consider the variable
aas an integer,&a(address ofa) is the location and it contains the value ofain that location. When you usea, you will get the value from the address.A pointer variable
p, when used, will also get a value from a location. But the value at that location is another location from which you can get a value.So let’s say you have:
In this example,
ais the variable on the stack at location 0x1234 andpis on the stack at location 0x1236 (a 16-bit int/pointer system). What you have in memory is:When you use
awith, for example:the value of
aat memory location 0x1234 is used to setb. However, with a pointer:you first look up the value of
pin memory location 0x1236 (the value is 0x1234), then dereference it (with ‘*’) to get the value ofa.