Explaining Pointers in C++

Explaining Pointers in C++
Techiio-author
Written by Sagar RabidasJanuary 31, 2022
8 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss explaining Pointers in C++

Pointers in C++

Pointers are the most effective tool in c++; it facilitates the programmer to get entry to and control the reminiscence without delay. As an instance, while a variable is created, the task of the compiler is to do memory allocation to save the cost of the variable. And this value has retrieved the usage of the variable call assigned to the information. C++ has the compatibility to shop and retrieve the facts from memory, referring to the deal with the memory vicinity at which the statistics are saved. C++ even plays hints on a pointer.

Why do we need Pointers in C++?

Dynamic reminiscence allocation is made simple in c++ the usage of guidelines, the maximum outstanding significance of recommendations is that they may be a whole lot more efficient in handling the extraordinary records sorts. Similarly, they boom the execution velocity whilst the characteristic returns one value and give a hand in gaining access to a variable described outdoor the function. The maximum commonplace usage consists of information management and gaining access to elegant member features.

How to Create Pointers in C++?

Here are the following steps to create pointers in C++

Step #1 – Initialization of pointers

It is advisable to initialize pointer variables as soon as they are declared. Since pointer variables store addresses, they can address any portion of the memory.

int    *a; // pointer to an integer
double *da; // pointer to a double
float *fa; // pointer to afloat
char   *ch   // character pointer

Step #2 – Pointer void

Here the pointer variable is allowed to point to any data type, and this type is used in passing pointers to functions that work independently of the data type that is pointed to.

  • Syntax: void * pointer variable;

Example:

#include<iostream.h>
#include <iostream>
using namespace std;
int main ()
{
int x, *iv;
float f, *fv;
void *vp;
x=3;
f=45.2;
iv=&x;
fv=&f;
vp=&x;
cout<< "the value pointed by iv is "<<*iv<< endl;
cout<< "The address of x is "<<iv <<endl;
cout<< "the value pointed by fv is "<<*fv<< endl;
cout<< "The address of f is "<<fv <<endl;
cout<< "The address of x is "<<vp <<endl;
vp= &f;
cout<< "the address of f is "<<vp<<endl;
}

Step #3 – Pointers Arithmetic Operations in C++

Pointer arithmetic is performed with arrays. The following operations can be performed on pointers. They are:

  • Increment (++)
  • Decrement (–)
  • Pointer addition
  • Pointer subtraction

When we add 1 to the pointer, it specifies adding the size of the pointer pointing at.

The below program specifies pointer arithmetic; it works until it gets at the end of the array.

#include <iostream>
#include <string>
using namespace std;
void pointerarithmetic(int a[], int size)
{
int *e, *t; //Declaring  two int pointers variables
e = a; //assigning e to point the arrays initial element a[0] t = a + size; // assigning variable t to the array last element
while(e != t)
{
cout << *e << endl; //displays the e
e++; // incrementing ( next element)
}
}
int main()
{
int a[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
pointerarithmetic (a, 20);
return 0;
}

Step #4 – Pointer to Pointer

float **FPP;

It denotes two levels of pointers (Multiple indirections). It’s a variable that points to another pointer further points to an object specified in a memory location. For example, FPP be afloat pointer currently pointing to memory address 2001, the size of the float is 8 bytes, then by

fpp++;

indicates fpp pointing to address 2009. Similarly, when the variable is decremented by 1, it will point to the previous location of its base type at address 1993.

Step #5 – Pointer to functions

When suggestions are surpassed to a function as arguments, the records gadgets related to those suggestions’ variables are altered inside the function and then lower back to the calling program; the modifications could be retained within the calling software. When a pointer is surpassed as a parameter, the respective facts objects are altered globally from in the referred to as feature. The pointer is passed by reference. Functions can be performed in pointers in different ways:

  1. The function invoked by passing a pointer
  2. The function invoked by passing the reference

In this, the address is passed as an argument instead of values.

Example:

#include <iostream>
using namespace std;
void changefn(int*, int*);
int main()
{
int n = 5, m = 6;
cout << "Before change" << endl;
cout << "n = " << n << endl;
cout << "m = " << m << endl;
changefn(&n, &m);
cout << "\nAfter change" << endl;
cout << "n = " << n << endl;
cout << "m = " << m << endl;
return 0;
}
void changefn(int* x1, int* x2) {
int s1;
s1 = *x1;
*x1 = *x2;
*x2 = s1;
}

C
C++
Pointers in C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance