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 << *p << endl; // Output: 10
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;
.
#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 << "a = " << a << endl; // print address of a cout << "Address of a (&a) = " << &a << endl; // print pointer p cout << "p = " << p << endl; // print the content of the address p points to cout << "Content of the address pointed to by p (*p) = " << *p << endl; return 0; }
Output :
a = 10 Address of a (&a) = 0x61ff10 pointVar = 0x61ff10 Content of the address pointed to by p (*p) = 10
#include <iostream> using namespace std; int main() { int a =10; int* p; // store address of a p = &a; // print a cout << "a = " << a << endl; // print *p cout << "*p = " << *p << endl ; cout << "Changing value of a to 17:" << endl; // change value of a to 17 a = 17; // print a cout << "a = " << a << endl; // print *p cout << "*p = " << *p << endl; cout << "Changing value of *p to 19:" << endl; // change value of a to 19 *p = 19; // print a cout << "a = " << a << endl; // print *p cout << "*p = " << *p << endl; return 0; }
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
int *p;
should be read as “p is a pointer to an
integer” and and not integer pointer p.
4 bytes
.
*
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”.
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;