The Ultimate Step-by-Step Guide to Learn Pointers in C++

Pointers are one of the finest features of C++. Hence, it is essential to understand pointers in C++, so that you can make use of those features. A pointer works with memory addresses rather than the variables, which makes it more interesting. This tutorial will help you learn all you need to know about C++ pointers.

Why Do You Need Pointers in C++?

Pointers provide many capabilities to the C++ language. It offers features like referring to the same space in memory from over one memory location, i.e. you can change one location in the program, and it can reflect those changes in other parts of the program. Because of pointers, it is possible to have a more flexible memory allocation and deallocation. And you also use it in polymorphism and function pointers.

Now, understand what pointers in C++ are.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What is a Pointer in C++?

In C++, every variable has its unique address or location in the computer's memory, and this special address is called memory address. You can define a pointer as the variable that holds the memory address of some other variable. It allows the developer to deal with the memory.

Pointers_Example1

Here 2000 is the address of a variable stored by the pointer, and 22 is the variable's value.

Declaration and Initialization of a Pointer

The basic syntax for the pointer in C++ is:

Syntax:         

Pointers_Example2.

Here, the data type can be int, char, double, etc. The pointer name can be anything with the * sign. The * operator declares the variable is a pointer.

Example: 

Pointers_Example3

Initialization

Example: 

Pointers_Example4      

Here ‘a’ is the variable of data type int, and 30 is the value that is assigned to this variable a. Pointer ptr is referring directly to the value a, so here ptr will store the address of variable a. &(address-of operator) is used to acquire the address of data stored in variable a.

With this, the concept of pointers in C++ and its syntax is clear. So now you will learn about the dereference operator.

Dereference Operator in Pointer

As you understood, the pointer stores the address and not the data, but how can you gain access to the data then? You can do it with the use of a dereference operator *. It provides you access to the data that is stored at the memory address.

Pointers_Example5

When you use the dereference operator with ptr, it will point to the memory address of variable a, i.e. 2000, which is also its own(ptr) value. Then, it will point to the value stored at the memory address, i.e. 22.

Example:

Pointers_Example6

In this example, pointer ptr is storing the address of the variable a. With the help of a dereferencing operator, you print the value of a, i.e. 50. Using *ptr, you access the value that is at the address available in the pointer.

Attached below is the output of the above example.

Pointers_Example7

Pointer Arithmetic

There are some arithmetic operations that you can perform on a pointer in C++ because the pointer stores an address which is a numeric value. And the arithmetic operators are:

  • Increment Operator (++)
  • Decrement Operator (--)
  • Addition (+)
  • Subtraction (-)

Increment Operator: When you increment a pointer, the size of its type increments its address. For example, for incrementing an integer pointer whose address is 450, after increment, it will be 454 because the int type size is 4 bytes.

Decrement Operator: When you decrement the pointer, its address will be decremented by the size of its type. 

Both increment and decrement operations on a pointer are used to traverse through the array because, on increment, the pointer will point to the following memory address of the next element. On decrement, it will point to the previous memory address.

Addition: When you perform an addition operation to the pointer ptr by 1, i.e. ptr+1, then it will point to the next memory address. Similarly, if you add 3 to ptr, then it will point to the address that is three times the size of the type of pointer, 3* (size of) type.

Subtraction: When you subtract on a pointer, it will decrease the address by n* (size of) type.

Now you will learn how to use the C++ pointer to access elements of the array.

Using Pointers to Access Elements of the Array

In an array, the name is referred to as the address of array inside the memory and that is why while assigning the address of an array to a pointer we don’t use ampersand sign &, because the array name denotes the address of the first element in the array.

So, to access the elements of the array, you can use a dereference operator with the array name.

For example:

Pointers_Example8

Here the array name is ‘records’ having five elements inside it. With the help of the array name, you print each element of the array because, as you know, ptr+1 will point to the next memory address. Similarly, records+1 will point to the next address, but you use the dereference operator along with it, that is why the value gets printed for that address. The same goes for records+2, records+3, and so on.

Below is the output of the above example.

Pointers_Example9

You can also traverse through array elements with the help of the increment operator.

Pointers_Example10

In this example, you will assign the array records to the pointer ptr, and with the help of a for loop, you then traverse the elements from 0 to 4. Inside the for loop, the values stored at the address of the pointer ptr will be printed one by one after each iteration. The ptr++ will help to move the pointer to the next position after each iteration.

Below is the output of the mentioned example.

Pointers_Example11

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Passing Pointers to Functions

In C++, you can also pass pointers as arguments for the functions. When you pass pointers to the function, then the address of the actual argument is copied to the formal argument of the called function. And that is why it reflects changes in the original variables.

Have a look at an example.

Pointers_Example12

In this example, you pass the address of two variables n1 and n2, to the swap function. So this function takes two arguments as pointers, i.e. ptr1 and ptr2. It took an integer variable temp inside this function and assigned it the value of *ptr1, then you assigned the value of *ptr2 to *ptr1. Now temp has the value of *ptr1, which you give to *ptr2. So that is how the values are now swapped.

Since it now swapped the values with pointers, it should also reflect this swapped value outside the function.

Pointers_Example13.

As you can see in the above output, they swap values.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

After reading this tutorial on C++ Pointers, you would have understood why you use pointers in C++, what is a pointer in C++, its declaration and initialization, and about the dereference operator in C++. You also learned about pointer arithmetic and how you can use pointers to access elements of the array with the help of some examples.

If you are perhaps looking to build a career in software development, do check the Post Graduate Program in Full Stack Development by Simplilearn. It can prove to be the ideal solution to help you build your career in the right way.

Do you have any questions regarding this tutorial on Pointers in C++? If you do, then please put them in the comments section. We’ll help you solve your queries. To learn more about Pointers in C++, click on the following link: Pointers in C++  

Happy learning!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.