Learning Pointers By Example
Hi I came across this bit of C++ code and am trying to learn how po开发者_JAVA技巧inters operate.
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}
My questions are below:
- what exactly happens in memory when
int *mypointer;
is declared? - what does
mypointer
represent? - what does
*mypointer
represent? - when
*mypointer = 10;
what happens in memory?
- what exactly happens in memory when int *mypointer; is declared?
Sufficient memory is allocated from the stack to store a memory address. This will be 32 or 64 bits, depending on your OS.
- what does mypointer represent?
mypointer
is a variable on the stack that contains a memory address.
- what does *mypointer represent?
*mypointer
is the actual memory location pointed to by mypointer
.
- when *mypointer = 10; what happens in memory?
The value 10
is stored in the memory location pointed to by mypointer
. If mypointer
contains the memory address 0x00004000
, for example, then the value 10
is stored at that location in memory.
Your example with comments:
int main ()
{
int firstvalue, secondvalue; // declares two integer variables on the stack
int * mypointer; // declares a pointer-to-int variable on the stack
mypointer = &firstvalue; // sets mypointer to the address of firstvalue
*mypointer = 10; // sets the location pointed to by mypointer to 10.
In this case same as firstvalue = 10; because
mypointer contains the address of firstvalue
mypointer = &secondvalue; // sets mypointer to the address of secondvalue
*mypointer = 20; // sets the location pointed to by mypointer to 10.
In this case same as secondvalue = 20; because
mypointer contains the address of secondvalue
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}
Try this code and see if that helps:
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
cout << "mypointer is pointing to " << mypointer << endl;
mypointer = &firstvalue;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
cout << "mypointer is pointing to " << mypointer << endl;
*mypointer = 10;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
cout << "mypointer is pointing to " << mypointer << endl;
mypointer = &secondvalue;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
cout << "mypointer is pointing to " << mypointer << endl;
*mypointer = 20;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
cout << "mypointer is pointing to " << mypointer << endl;
return 0;
}
- This will create a pointer to a location in memory. By defining it at int*, we're saying that when we dereference this pointer, the bytes should be translated as an int.
- myPointer points to a location in memory. If you access myPointer by itself, it will give you address where the data it points to is stored.
- *myPointer dereferences the point to give the value that pointer points to.
- *myPointer = 10: take the value 10 and store it in memory where myPointer points.
int *mypointer;
it is a declaration.. it tells to the compiler that we are going to use mypointer as a integer pointer variable.
mypointer = &firstvalue;
here the address of the firstvalue is stored in mypointer.
*mypointer = 10;
consider mypointer points to the address location 4000. now the value 10 is stored in address location 4000.
any other doubt please communicate with me
what exactly happens in memory when int *mypointer; is declared?
mypointer
is declared to hold the address of an int. So, number of bytes required to hold an integer's address is allocated for mypointer
.
what does mypointer represent?
It a variable that can hold an integer variable's address.
what does *mypointer represent?
It is dereferencing the pointer. Getting the value at the address location mypointer
holds.
when *mypointer = 10; what happens in memory?
Assigning value 10
at the location mypointer
holds.
- When you declare
int *mypointer
it declares a pointer type (a long) that could contain the memory address of an integer. mypointer
represents an address to an integer in much the same way that a street address represents a house on a street.*mypointer
dereferences the address and points to the actual value of the int. This is analagous to the actual house that the address points to.- in memory, the value at the memory location to which mypointer points is updated.
精彩评论