Pointers are variables that store the index values of other variables. In this article, I will introduce you to the concept of Pointers in C ++ programming language.
Introduction to Pointers in C++
A pointer is an address that refers to a location in memory. They are commonly used to allow functions or data structures to know and modify memory without having to copy the memory in question. Pointers can be used with primitive (built-in) or user-defined types.
Pointers use the “dereference” *, “address of” & and “arrow” -> operators. The ‘*’ and ‘->’ operators are used to access pointed memory, and the & operator is used to get an address in memory.
There are two operators for pointers: Address operator (&): returns the memory address of its operand. Contents-of operator (Dereference) (*): returns the value of the variable located at the address specified by its operator.
Getting Started with Pointers
Every variable is stored in the memory and each memory location has its memory address. It enables us to pass variables by reference. There are two things that you need to remember while working with Pointers:
- ‘&’ Operator: It gives the address of the variable.
- ‘*’ Operator: It gives the value stored at the address, i.e dereferences the value stored at the address
Let’s see how to implement it in code:
The asterisk (*) is used to declare a pointer for the simple purpose of indicating that it is a pointer. Do not confuse it with the dereference operator, which is used to get the value located at the specified address. They are just two different things represented by the same sign.
I hope you liked this article on the concept of Pointers in C++ programming language. Feel free to ask your valuable questions in the comments section below.