The computer’s memory is a sequential collection of storage cells. Each cell, commonly known as a byte, has a number called address associated with it. Typically, the addresses are numbered consecutively, starting from zero. The last address depends on the memory size. A computer system having 64k memory will have its last address as 65,535.
Whenever we declare a variable in our programs, the system allocates somewhere in the memory, an appropriate location to hold the value of the variable.
The above statement int var creates a location in the memory to hold integer value. That location will have an address for example assume it is 5000. The statement var = 200 stores the value 200 at the location whose address is 5000. So, in our example, var is the variable name, 200 is the value stored in var and 5000 is the address of the memory location containing the variable var.
So, we can access the value 200 either by using the variable name var or by using the address of the memory location which is 5000. We can access using the second method by storing the address in another variable. This concept of storing memory address in a variable and accessing the value available at that address is known as a pointer variable. Since the pointer is also variable, it will also have a memory address just like any other variable.
The memory addresses within a computer are referred to as pointer constants.
We cannot assign the memory addresses directly to a variable. We can only get the address of a variable by using the address operator (&). The value thus obtained is known as pointer value.
Once we obtain the pointer value, we can store it in a variable. Such variable which stores the memory address is known as a pointer variable.
Accessing the Address of a Variable
The actual location of a variable in memory is system dependent. A programmer cannot know the address of a variable immediately. We can retrieve the address of a variable by using the address of (&) operator. Let’s look at the following example:
int a = 10;
p = &a;
In the above example, let the variable a is stored at memory address 5000. This can be retrieved by using the address of operator as &a. So the value stored in variable p is 5000 which is the memory address of variable a. So, both the variable a, and p point to the same memory location.
Take your time to comment on this article.