Basics of Pointers in C++ (A Simple Tutorial)

Pointers in C++ is one of the core concepts that you need to learn. This is because it is a feature that helps you understand how your program code interacts with memory. So let’s get started.

  1. What are Pointer?
  2. Declaring and Assigning a Pointer
  3. Accessing Value in a Pointer (Dereferencing)
  4. The Complete Program
  5. Watch the Video Tutorial

1. What are Pointer?

A pointer is simply the address of a variable in memory. Let’s clarify this. When you assign a variable in C++, what you are doing is creating a location in memory and placing a value in there. For example, if you say

  int num = 45;

 

The number 45 is stored in a memory location which out program labels num. However the computer keeps the address of this memory location. So a pointer simply keeps the address of memory locations. To view this address, we would use the ampersand (&) symbol with the name of the variable.  The code below displays the address of num

  int num = 45;
  cout<<&num;

 

The above code would display: 0x61ff1c

 

2. Declaring and Assigning a Pointer

Now assuming we want to store this address in some variable. We cannot store it on just normal variables. We can only store it in pointer variables. A pointer variable (or just pointer) is used to hold addresses of other variable. Before you can use a pointer variable, you must declare it like this:

int *np;		//Declare a pointer
np = &num;		//Assign a value to the pointer

 

This code  would declares a pointer np. Simply by adding an asterisk (*) to the variable, we are declaring it as a pointer. So when we want to use it we could just use it without the  *.

 

3. Accessing the Value in a Pointer

Just as we mentioned before, a pointer is an address. So if we have a pointer, we could access the value stored in this address. To do this, we use the dereferencing operator(which is also an asterisk) with the name of the pointer.

The code below would give us the actual value stored in the pointer.

 cout<<*np;

 

The Complete Program

I have made a complete program with a line by line explanation of how to work with pointers in C++. Feel free to copy this program and run it to make sure you are clear with how pointers work.  In  a different lesson, we would examine pointers to pointers.

//============================================================================
// Name        : Pointers Tutorial in C++
// Author      : Kindson The Tech Pro
// Copyright   : Your copyright notice
// Date 	   : 28th December, 2018
//============================================================================

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

int main(void) {

	int num = 45;	//Declare an int variable num and assing a value of 45
	int *np;		//Declare a pointer(address of an int variable)
	np = &num;		//Assign an address to the pointer
	cout<<np<<endl;	//Display the pointer(address)
	cout<<*np;		//Display the value in the address(dereferencing)

	return 0;
}

If you run this program correctly, then your output would be as shown below.



Watch the Video

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

One thought on “Basics of Pointers in C++ (A Simple Tutorial)

  1. Thanks for sharing this step by step information about C++ and read your complete post, you have explained in really very understanding method so keep posting such informative blog post.

Leave a Reply

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