In this article, we are going to learn about pointers , its syntax and some special points about it.
As from before discussion we know that every variable in C++ when declared have a unique address in the memory. What if we want to store these address. It is not possible for a normal variable to store the address of other variable so here the concept of pointer comes.
Pointers are those special variables which are meant for storing addresses of other variables.
General syntax for declaring a pointer
<data_type>* <pointer_name>
Pointers data type is decided by seeing the data type of variable whose address is to be stored in that pointer i.e. pointers data type should be same as the data type of the variable it is pointing to.
Here is how we can declare pointers.
int *p;
Here, we have declared a pointer p of the type int.
We can also declare pointers in the following way.
int* p; // preferred syntax
Note : The * operator is used after the data type to declare pointers.
Here is how we can assign addresses to pointers:
int* p ,a=10; // assign address of var to pointVar pointer p = &a;
Here, 10 is assigned to the variable a. And, the address of a is
assigned to the p pointer with the code p = &a. In other words, pointer
p of integer type is pointing to the memory address of a.
To get the value pointed by a pointer, we use the * operator this operator is also known as dereferencing operator. For example:
int* p, a=10;
// assign address of a to p
p = &a;
// access value pointed by p
cout
In the above code, the address of a is assigned to the p pointer. We have used
the *p to get the value stored in that address.
When * is used with pointers, it's called the dereference operator. It
operates on a pointer and gives the value pointed by the address stored in
the pointer. That is, *p = a.
Note : In C++, p and *p is completely different. We
cannot do something
like *p = &a;.
Working of C++ Pointers
#include <iostream>
using namespace std;
int main() {
int a = 10;
// declare pointer variable
int* p;
// store address of a
p = &a;
// print value of a
cout
Output :
a = 10
Address of a (&a) = 0x61ff10
pointVar = 0x61ff10
Content of the address pointed to by p (*p) = 10
Changing Value Pointed by Pointers
#include <iostream>
using namespace std;
int main() {
int a =10;
int* p;
// store address of a
p = &a;
// print a
cout
Output :
a = 10
*p = 10
Changing value of a to 17:
a = 17
*p = 17
Changing value of *p to 19:
a = 19
*p = 19
Some special points about pointers :
-
The data types of the pointers is always kept same as the data types
of variables to whom the pointer will point.
-
Formally, we must read the declaration of a pointer from right to left.
So the following line:
int *p; should be read as “p is a pointer to an
integer” and and not integer pointer p.
-
The size of the pointer never depends on its data type. The size of
pointer in turbo compiler is of 2 bytes and in GCC compiler it is of
4 bytes.
-
Whenever we use the symbol of
* with the pointer name declaration,
we read it in a very special way. The expression:
*p;
is read as “value at address stored in p” or simply “value at p”.
Common mistakes when working with pointers
Suppose, we want a pointer p to point to the address of a. Then,
int a, *p;
// Wrong!
// p is an address but a is not
p = a;
// Wrong!
// &a is an address
// *p is the value stored in &a
*p = &a;
// Correct!
// p is an address and so is &a
p = &a;
// Correct!
// both *p and a are values
*p = a;