C Pointers Module
C Pointers Module
In C++, pointers are variables that store the memory addresses of other
variables.
Address in C++
Every variable we declare in our program has an associated location in the
memory, which we call the memory address of the variable.
If we have a variable var in our program, &var returns its memory address. For
example,
#include <iostream>
using namespace std;
int main()
{
// declare variables
int var1 = 3;
int var2 = 24;
int var3 = 17;
Output
Address of var1: 0x7fff5fbff8ac
Address of var2: 0x7fff5fbff8a8
Address of var3: 0x7fff5fbff8a4
Note: You may not get the same results when you run the program. This is
because the address depends on the environment in which the program runs.
C++ Pointers
Here is how we can declare pointers:
int *point_var;
Here, 5 is assigned to the variable var . And the address of var is assigned to
the point_var pointer with the code point_var = &var .
int var = 5;
In the above code, the address of var is assigned to point_var . We have used
the *point_var 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, *point_var = var .
return 0;
}
Output
var = 5
Address of var (&var) = 0x61ff08
point_var = 0x61ff08
Content of the address pointed to by point_var (*point_var) = 5
For example,
int var = 5;
int* point_var = *var;
Here, point_var and &var have the same address; the value of var will also be
changed when *point_var is changed.
// print var
cout << "var = " << var << endl;
// print *point_var
cout << "*point_var = " << *point_var << endl
<< endl;
// print var
cout << "var = " << var << endl;
// print *point_var
cout << "*point_var = " << *point_var << endl
<< endl;
// print var
cout << "var = " << var << endl;
// print *point_var
cout << "*point_var = " << *point_var << endl;
return 0;
}
Output
var = 5
*point_var = 5
int var = 5;
// Wrong!
// point_var is an address but var is not
int* point_var = var;
// Wrong!
// &var is an address
// *point_var is the value stored in &var
*point_var = &var;
// Correct!
// point_var is an address and so is &var
point_var = &var;
// Correct!
// both *point_var and var are values
*point_var = var;
int *ptr;
int arr[5];
// store the address of the first
// element of arr in ptr
ptr = arr;
Here, ptr is a pointer variable while arr is an int array. The code ptr =
arr; stores the address of the first element of the array in variable ptr .
Notice that we have used arr instead of &arr[0] . This is because both are the
same. So, the code below is the same as the code above.
int *ptr;
int arr[5];
ptr = &arr[0];
The addresses for the rest of the array elements are given
by &arr[1] , &arr[2] , &arr[3] , and &arr[4] .
Here, if ptr points to the first element in the above example then ptr + 3 will
point to the fourth element. For example,
int *ptr;
int arr[5];
ptr = arr;
Similarly, we can access the elements using the single pointer. For example,
// use dereference operator
*ptr == arr[0];
*(ptr + 1) is equivalent to arr[1];
*(ptr + 2) is equivalent to arr[2];
*(ptr + 3) is equivalent to arr[3];
*(ptr + 4) is equivalent to arr[4];
Working of C++
Pointers with Arrays
#include <iostream>
using namespace std;
int main()
{
float arr[3];
// ptr = &arr[0]
ptr = arr;
return 0;
}
Run Code
Output
In the above program, we first simply printed the addresses of the array
elements without using the pointer variable ptr .
Then, we used the pointer ptr to point to the address of a[0] , ptr + 1 to point
to the address of a[1] , and so on.
However, we should remember that pointers and arrays are not the same..
#include <iostream>
using namespace std;
int main() {
float arr[5];
return 0;
}
Output
Here,
1. We first used the pointer notation to store the numbers entered by the user
into the array arr .
Notice that we haven't declared a separate pointer variable, but rather we are
using the array name arr for the pointer notation.
As we already know, the array name arr points to the first element of the
array. So, we can think of arr as acting like a pointer.
2. Similarly, we then used for loop to display the values of arr using pointer
notation.