Pointers

When you declare a variable, the OS allocates space in memory where the value associated with this variable-name would be stored. It's like creating a box in memory and putting a tag with the variable-name written on it.

A picture which shows this

 

 

When you actually assign a value to this variable, for example

MyInteger = 67;
MyDouble = 17.32;

the value 67 is written in the memory location associated with MyInteger; and 17.32 in MyDouble.

 

 


 

A pointer is the address of a particluar location in memory. Declaring an integer pointer, for example, does the following:

int *MyIntPtr;
creates a location in memory which would hold the address of an integer residing somewhere else in memory:

 


MyIntPtr = &MyInteger;
makes MyIntPtr point to MyInteger.

 

 

Now we can use MyIntPtr to modify the value that's stored in MyInteger.

Example
#include void main( ) { int *MyIntPtr; int MyInt; MyInt = 67; MyIntPtr = &MyInt; printf ("\nMyInt = %d\n*MyIntPtr = %d\n", MyInt, *MyIntPtr); *MyIntPtr = 23; printf ("\nMyInt = %d\n*MyIntPtr = %d\n\n", MyInt, *MyIntPtr); }

Here's the output:

UNIX prompt >> a.out

MyInt = 67
*MyIntPtr = 67

MyInt = 23
*MyIntPtr = 23

UNIX prompt >>

 


More Examples