C++ Pointers - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Saturday 7 October 2017

C++ Pointers

C++ Pointers




For a C++ program, computer memory is like succession memory cells, each holding one byte size and having 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 efficiency of program and allow programmers to handle unlimited amount of data. It is possible for pointers to dynamically allocate memory, where programmers don’t have to be worry about how much memory they will need to assign for each task, which cannot be done / performed without the concept of 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 program.
  • Pointers allows passing of arrays and strings to functions more efficiently.
  • Pointers makes 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;
}
Program Output:
cplusplus-pointers


No comments:

Post a Comment

Post Top Ad