C++

In C++, Pointers – Detailed Instructions From [A-Z]

In C++, a pointer is a variable that stores the location of another variable. They possess the same data types as variables; for instance, a character type pointer can store the address of a char variable whereas an integer type pointer can store the address of an integer variable.

Syntax of pointer

data_type *pointer_name;

How to declare a pointer?

/* This pointer p can hold the address of an integer 
 * variable, here p is a pointer and var is just a
 * simple integer variable
 */
int *p, var

Assignment

An integer type pointer can store the location of another int variable, as I indicated previously. Here, the address of the integer variable var is held by pointer p. Ampersand symbol is used to assign the address of a variable to a pointer (&).

/* This is how you assign the address of another variable
 * to the pointer
 */
p = &var;

How to use it?

// This will print the address of variable var
cout<<&var;    

/* This will also print the address of variable
 * var because the pointer p holds the address of var
 */
cout<<p;    

/* This will print the value of var, This is 
 * important, this is how we access the value of
 * variable through pointer
*/
cout<<*p;

Example of Pointer

To better grasp what we talked about earlier, let’s use a straightforward example.

#include <iostream>
using namespace std;
int main(){
   //Pointer declaration
   int *p, var=101;
 
   //Assignment
   p = &var;

   cout<<"Address of var: "<<&var<<endl;
   cout<<"Address of var: "<<p<<endl;
   cout<<"Address of p: "<<&p<<endl;
   cout<<"Value of var: "<<*p;
   return 0;
}

Output:

Address of var: 0x7fff5dfffc0c
Address of var: 0x7fff5dfffc0c
Address of p: 0x7fff5dfffc10
Value of var: 101

Pointer and arrays

There are a few considerations you should make when handling arrays with pointers. The first and most crucial thing to remember about arrays is that the array name alone indicates the base address of the array, thus avoid using the ampersand symbol when giving the address of the array to the pointer (&). Follow these steps:
True: Since arr represents an array’s address.

p = arr;

Incorrect:

p = &arr;

Example: Using Pointers to navigate the array

#include <iostream>
using namespace std;
int main(){
   //Pointer declaration
   int *p;
   //Array declaration
   int arr[]={1, 2, 3, 4, 5, 6};
   //Assignment
   p = arr;
   for(int i=0; i<6;i++){
     cout<<*p<<endl;
     //++ moves the pointer to next int position
     p++;
   }
   return 0;
}

Output:

1
2
3
4
5
6

How can I increase the value and address of a pointer?

When using a pointer to retrieve a variable’s value, we may occasionally only need to increase or decrease the value of the variable through it or move the pointer to the next int place (just like we did above while working with arrays). This is accomplished by using the ++ operator. One of the ++ operator examples we saw earlier involved traversing the array with a pointer while using the ++ operator to increase the pointer value. Here are a few more examples.

// Pointer moves to the next int position (as if it was an array)
p++; 
// Pointer moves to the next int position (as if it was an array)   
++p;   

/* All the following three cases are same they increment the value 
 * of variable that the pointer p points.
 */
++*p;   
++(*p); 
++*(p);

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button