Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Pointers in C++

What are the Pointers in C++?

Pointers the most powerful tool in c++, it helps the programmer to access and manipulate the memory directly.  For instance, when a variable is created, the job of the compiler is to do memory allocation to store the value of the variable. And this value is retrieved using the variable name assigned to the data. C++ has the compatibility to store and retrieve the data from the memory referring to the address of the memory location at which the data is stored. C++ even perform Pointers on a Pointer.

Syntax

The general format of the pointer declaration is:

Data_type * pointer -variable-name

Note that the pointer variable should be preceded by an asterisk (*)

Example: int * xptr;

The variable xptr is a pointer to an integer. In general, pointer variables can point to integer variables, character variables, arrays, files, functions.

Why do we need Pointers in C++?

Dynamic memory allocation is made simple in C++ using pointers, the most prominent importance of pointers is they are much efficient in handling the different data types. They increase the execution speed when the function returns one value and also give hand in accessing a variable defined outside the function. The most common usage includes data management and accessing class member functions.

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

Consider the following example:

int p, *pi; // This statement instructs the compiler to reserve a space for the variable p in memory to hold an integer value.

pi=&a;  // Assigns the address of the integer variable p to the pointer variable. For instance, if the address of p is 4581, then the value in the *pi will be equal to 4581.

Step #2 – Pointer void

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

Syntax: void * pointer variable;

Example:

#include
#include
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 cout cout cout cout vp= &f;
cout }

Output:

$g++ -o main *.cpp
$main
the value pointed by iv is 3
The address of x is 0x7ffefbbee6d4
the value pointed by fv is 45.2
The address of f is 0x7ffefbbee6d0
The address of x is 0x7ffefbbee6d4

the address of f is 0x7ffefbbee6d0

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
#include
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++; // incrementing ( next element)
}
}
int main()
{
int a[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
pointerarithmetic (a, 20);
return 0;
}

Output:

$g++ -o main *.cpp
$main
2

4

6

8

10

12

14

16

18

20

0

0

4196480

0

-1743362854

32686

1

0

153860328

32766

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 a float 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 pointers are passed to a function as arguments, the data items associated with these pointers’ variable are altered within the function and then returned to the calling program, the changes will be retained in the calling program. When a pointer is passed as a parameter, the respective data items are altered globally from within the called function. The pointer is passed by reference. Functions can be performed in pointers in different ways:

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

The function invoked by passing the reference

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

Example:

#include
using namespace std;
void changefn(int*, int*);
int main()
{
int n = 5, m = 6;
cout cout cout changefn(&n, &m);
cout cout cout return 0;
}
void changefn(int* x1, int* x2) {
int s1;
s1 = *x1;
*x1 = *x2;
*x2 = s1;
}

Output:

$g++ -o main *.cpp
$main
Before change

n = 5

m = 6

After change

n = 6

m = 5

Conclusion

This article intended to refresh the knowledge about how to use pointers in C++ and their basic topics in a simple way with an example. Pointer also is known as locator reduces the code statement for higher performance. Pointers plays a vital role in implementing data structures like linked list and programming in the system level. They are the most preferable language in embedded systems as they are a good way to access memory directly using pointers.

Recommended Articles

This is a guide to Pointers in C++. Here we discuss how to Create Pointers in C++ with the given examples and output and Why do we need it. You may also look at the following data analysis course to learn more

  1. Pointers in Python
  2. Data Types in C
  3. Arrays in C++
  4. Star Patterns In c++

The post Pointers in C++ appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Pointers in C++

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×