Pointer Definition in C++

For a C++ program, computer memory is like succession memory cells, each holding one-byte size and having a unique address. Dealing with these memory addresses in programming is done by pointers. Pointers are extremely powerful programming tool that can make things easier and help to increase the efficiency of the program and allow programmers to handle the unlimited amount of data. It is possible for pointers to dynamically allocate memory, where programmers don’t have to worry about how much memory they will need to assign for each task, which cannot be done/performed without the concept of the pointer. Hence pointer is considered as the most distinct and exciting feature of C++ language which adds power as well as flexibility to the language. In other words, a pointer variable holds the address of a memory location.

Pointer Definition in C++

Syntax:
type *variable_name;
Example:
int  *height;char  *age;



Benefits of using Pointers in C++

  • Pointers save the memory.
  • Pointers reduce the length and complexity of the program.
  • Pointers allow passing of arrays and strings to functions more efficiently.
  • Pointers make possible to return more than one value from the function.
  • Pointers increase the processing speed.

How to use Pointers in C++

Example:
#include <iostream>using namespace std;int main (){   int  n = 20, *ptr;  /* actual and pointer variable declaration */   ptr = &n;  /* store address of n in pointer variable*/    cout << "Address of n variable: " << &n << endl;      /* print address stored in pointer variable */   cout << "Address stored in pntr variable: " << ptr << endl;   /* print access the value using the pointer */   cout << "Value of *pntr variable: " << *ptr << endl;      system("pause");   return 0;}




1 reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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

five − 2 =