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 variab开发者_JAVA技巧le, 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 a
as an integer, &a
(address of a
) is the location and it contains the value of a
in that location. When you use a
, 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:
int a = 7; // set a to 7.
int *p = &a; // set p to the address of a.
In this example, a
is the variable on the stack at location 0x1234 and p
is on the stack at location 0x1236 (a 16-bit int/pointer system). What you have in memory is:
+--------+
0x1236 (p) | 0x1234 |
+--------+
0x1234 (a) | 0x0007 |
+--------+
When you use a
with, for example:
int b = a;
the value of a
at memory location 0x1234 is used to set b
. However, with a pointer:
int c = *p;
you first look up the value of p
in memory location 0x1236 (the value is 0x1234), then dereference it (with '*') to get the value of a
.
"2 parts" is a bit of a strange way to talk about it. A variable has a value, and is located somewhere in the memory. So, at address 0x0004, for example, you could have value 7 (variable v).
A pointer is a variable that contains an address. So, at address 0xff0c for example, you could have the value 0x0004 (pointer p).
In C:
v is 7
&v (address of v) is 0x0004
p is 0x0004
&p (address of p) is 0xff0c
*p (element pointed by p) is 7
In practice, you never have to worry about the actual addresses, just the relationships. Don't hesitate to write a few programs with very simple variables and pointers to variables, and print values, addresses and dereferences, I found that it helps a lot in clarifying pointers.
A normal variable, say an int, has a value, and is at a certain place in memory. That place in memory isn't stored in the variable, it is just its address.
A pointer is just like an int. It has a value (which is the address of another variable), and a location, which is just its address. A pointer to that pointer will hold the address to the first pointer.
a = 100;
the pointer a (aka &a) is a memory location, let's say 0x1000000, which contents is 100.
You can take a look at these slides to clarify!
A piece of memory has an address and stores a value.
A pointer variable contains the address.
A dereferenced pointer variable contains the value.
In C, for example, pointer value is an address of something. Usually address of some other variable. The values of pointer variables are exactly pointer values.
The pointer variables, in turn, have their addresses, so you can assign one pointer variable to be the address of another pointer variable. Or any variable you'd like.
Take a normal int:
int x ;
x = 400 ;
Now x has a value, and it also has.. an address
printf( "x's value=%d, x's memory location=%d\n", x, &x ) ;
Irrespective of what type the variable is, be it a pointer or an ordinary variable, all variables are places in memory which has a value in it. By saying places in memory, it also means that the place will have an address like 0x1234. So all variables will have an address and a value stored @ that address.
When you write just the variable, the compiler takes it that you're talking about the value holded in its address I.e. a = 12;
. Be it a
is a pointer or an oridnary variable, you're assigning 12 to it. Lets say a
is @ 0x1234, so this value of 12 will be stored at that place. When you prefix an &
before a variable name, it means you're talking of a variable's address, since &
is an operator in C and C++ which is called address-of (again applies for all kinds of variables); so &a
means address of a.
Difference:
Now for the difference bewteen pointers and normal variables. Pointers are variables who contain an address as it's value I.e. like a
has 12
contained in it, ptr
will have some value contained, which is an address in the memory and not a literal value. When it's a pointer variable, you're allowed to use the value-at operator *
as a prefix to say that "now am talking about the value stored at the location holded by this pointer variable". I.e. *ptr
means value @ the location pointed by ptr
. So when you do ptr = 12;
it means you're assigning 12 to ptr
and when you write *ptr = 1;
, it tries to put the value 1
to address 12
in the memory.
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
A variable contains only one piece of data: Its value. However, whenever we read that value, we also know its location (otherwise we wouldn't have been able to read the value). So it'd also be correct to say that every variable has an location. It's just not stored in the variable, but rather, the variable is stored in that location.
So a pointer variable is just the location part of a normal variable, and it doesn't have value itself?
No, a pointer is a "normal variable" too. It contains a value, and it is stored at a location. But in this case, its value is another location. But the pointer value is also stored at some location in memory. It has to be, in order to exist in the program.
Of course, the pointer doesn't contain the location it points to, any more than a letter contains the building it is addressed to. It simply contains the address "written down".
At address 0x16 we might have stored an integer. At address 0x42 we could then create a pointer, containing the value 0x16. The pointer now "points to" our integer. But it doesn't contain the integer, and the pointer itself also has an address. (So at address 0x20 we could create a pointer to the pointer, by storing the value 0x42 in it.
The key is that everything in memory is located at some address. And that address can be "written down" as a number. And that number can be stored at another address. And that's what a pointer is. A location containing the address of some other object.
A pointer contains the memory address of another variable, that is its value. A pointer also has its own memory address. By dereferencing the pointer, you can modify the contents of the variable it is pointing to.
One advantage of pointers, for instance on a 32bit platform, is that they are always 4 bytes large. So for instance, if you have loaded a large image file into memory, lets say it is 1 megabyte, and you want to have a function modify it, if you pass in a pointer to the image, that pointer will only be 4 bytes. If you didn't use a pointer, you would be passing in 1 megabyte!.
Pointer itself has a memory location as well. But inside that location, the value is an address to store other variable's location.
精彩评论