Introduction to Pointers

Pointer is an important concept in C/C++ programming.  Most of the time we might need the address of a variable to see where it is stored and how much memory it consumes. The concept of storing the address in a variable is possible with the help of a pointer.

Pointers are just like variables. Instead of holding the value, it holds the address of another variable. But it is defined in a different manner. The pointer variable is created using the *(asterisk) sign as shown in the example.

It should be carefully noted that the data type of the pointer variable should be same as that of the another variable, to which it is pointing to. For instance, an integer type pointer can hold the address of an integer variable, a character type pointer can hold the address of character variable.

Here is an example of integer pointer

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n = 50;
	int* pn;
	pn = &n;
	return 0;
}

In the above example, n is an integer variable, and pn is pointer to an integer. pn holds the address of the variable n. It should be noted carefully that, you should not leave the value of a pointer variable unassigned. It should be assigned the address of a variable.

Look at the next pointer program of C++

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n=50;
	int* pn;
	pn = &n;
	cout<<"n = "<<n<<endl;
	cout<<"&n = "<<&n<<endl;
	cout<<"pn = "<<pn<<endl;
	cout<<"*pn = "<<*pn<<endl;
	cout<<"&pn = "<<&pn<<endl;
	return 0;
}

The output of the program is shown below.

output of the above program
output of the above program

Following points can be noted from the above example

  • Value of &n and pn is same, i.e. pn stores the address of n.
  • Value of n and *pn is same, i.e. using *(de-referencing operator) gives the value of n.

To understand pointers effectively, we should know how the variables get stored in the memory. So, it is better to have a prerequisite knowledge about memory management in a computer.

Memory Management

To understand pointers effectively, we should know how the variables get stored in the memory. When we talk about memory in case of program execution, we talk about RAM (Random Access Memory). Your computer RAM can be 2Gb, 4Gb, 8Gb or more. The memory here is represented in the form of block.

Consider the block of memory shown below.

block of memory
block of memory

Each block of memory is 1 byte. And every block of the memory has a unique address. Addresses are mostly represented in hexadecimal form. For the sake of convenience, we represent it here in decimal form. The first address is 0 and as we progress, the address keeps on increasing as 0,1,2….

So, when we declare an int variable, for example say n. Then the computer allocates some memory as shown in the figure.

block of memory with int variable 'n'
block of memory with int variable ‘n’

The amount of the memory it allocates depends on the data type and also on a compiler. As we have declared n as int, 4 bytes of memory is allocated to n. If the data type of n would have been different, different bytes of memory would have been allocated. For instance, char allocation is 1 byte, float allocation is 4 bytes. Now, if we allocate the value of n to 10. 10 is stored in those 4 bytes of memory.

block of memory with integer value 10 occupying 4 bytes
block of memory with integer value 10 occupying 4 bytes

The value is written in the form of a binary number. So, now when you call the address of the variable n, we get the value 100. This is because the value starts from 100. And then it occupies the 4 byte 100,101,102, and 103 to store the value 10. Now, how does your computer knows that the value of n is at address 100?

For the same purpose, your computer has a look up table, that stores the variable name, its type and the address. So whenever the variable is called, the computer looks the address from that lookup table and provides you with the value of variable.

Say, now you define a new char variable c. You allocate the value of c to “a”. Every char variable takes 1 byte of memory. So, now it assigns c an address of 106. Hence, value of c is stored at 106.

block of memory with int and char variable
block of memory with int and char variable

Example

Let’s take an example of an integer variable n and its corresponding pointer variable pn. We can write the program as shown below.

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n=10;
	int* pn;
	pn = &n;
	cout<<"n = "<<n<<endl;
	cout<<"&n = "<<&n<<endl;
	cout<<"pn = "<<pn<<endl;
	cout<<"*pn = "<<*pn<<endl;
	cout<<"&pn = "<<&pn<<endl;
	return 0;
}
block of memory with n and pn
block of memory with n and pn

Above block is the representation of how the variable and pointer variable will be stored. The output of the above program based on above diagram will be(the output of memory address will be different, when you run it on a computer.)

n = 10
&n = 100
pn = 100
*pn = 10
&pn = 108

So, in the above case, the value of n is assigned 10. Pointer pn is a pointer to integer that stores the address of n. So, now if we print the value of n the value we get is 10. If we print the value of pn, the value will be 100, as it is the address of the variable n. We can also get the address of the variable using the & sign. So, &n and pn gives us the value of the address of n. Variable pn is also stored in memory. So, &pn gives us the value of the address of pn. In this case, we get the value 108, as shown in the figure. Now, if we print *pn, we get the value 10. This is known as de-referencing, which is discussed here.

Run the below program and see if the value changes using the pointer variable.

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n=10;
	int* pn;
	pn = &n;
        *pn = 50;
	cout<<"n = "<<n<<endl;
	cout<<"&n = "<<&n<<endl;
	cout<<"pn = "<<pn<<endl;
	cout<<"*pn = "<<*pn<<endl;
	cout<<"&pn = "<<&pn<<endl;
	return 0;
}

Below mentioned articles shows in-depth knowledge on pointers:

  • Examples on pointers
  • Working with pointers
  • Pointers to pointers and functions
  • Pointers and arrays
  • Dynamics memory allocation

3 Replies to “Introduction to Pointers”

Leave a Reply

Your email address will not be published. Required fields are marked *