0
Posted November 21, 2012 by Spyros in C/C++ Programming
 
 

C++ Pointers – Explained

german shorthaired pointer
german shorthaired pointer

Pointers in C++ have a very influential role to play. Though a bit complex, they are one of the most powerful tools in the programming language. The inclusion of pointers in a C++ program can pave the way for a number of beneficial things. Using pointers in coding helps increase the efficiency of the program, simplifies tasks, easily handles huge amount of data and assists in memory allocation processes, where the memory requirement of the program can be estimated beforehand, even while writing them. So, let’s further look and understand the functionalities of pointers and how they can be named and utilized in a program.

But, What are Pointers ?

Before we understand pointers and their usage, let’s take a quick recap of Arrays. The understanding of arrays in C++ is very essential to understand pointers and proceed further. Arrays are the series of entities of same type stored in consecutive memory locations. Each memory location signifies a byte and can be referenced or accessed with the help of an identifier.

In simple words, pointers are used to point the locations of variables or inputs in a system memory. The series of variables that are stored in sequential memory locations have a distinct address each. This distinct address forms the value of the specified pointer. For instance, let’s take the variable input “H” and ” I”, which are stored in a series of two consecutive memory locations of value A1 and A2 (illustrated only for understanding and does not resemble the actual). When you invoke a pointer, it will take up the value of A1 for the variable H and the value of A2 for the variable I. This can then be used to direct the values of variables directly without needing to specify the memory locations again and again. Another way to understand the concept is to assume pointers as the shortcuts in the desktop. These shortcuts point to the actual source file present in any of the directories in the system. They store the address of the file as their value and thus function similarly.

Syntax:

A Pointer in C++ always receives a special treatment from the compiler. The general syntax of a pointer is a bit different from the other regular syntaxes because, a pointer has to include the memory location and the value of that particular variable.  Also, when using a pointer, it is required by the programmer to specify the compiler that a pointer is used, with the help of an asterisk. So the general syntax of the command will look like this:

< the type of pointer> *<name of the pointer>

In the above syntax, the type of pointer represents the pointer type and indicates the variable types, the pointer can store under it. This includes functions like char, float, int and double. The next portion in the syntax is the asterisk symbol, which specifies the initiation of a pointer, followed by the name of the pointer. Usually, the pointer’s name depends up on the programmer and mostly ends with the characters ptr.

Now, let’s conclude with an example program:


#include <iostream>

using namespace std;

int main ()

{

  int  var = 20;   // actual variable declaration.

  int  *ip;        // pointer variable

  ip = &var;       // store address of var in pointer variable

  cout << "Value of var variable: ";

  cout << var << endl;

  // print the address stored in ip pointer variable

  cout << "Address stored in ip variable: ";

  cout << ip << endl;

  // access the value at the address available in pointer

  cout << "Value of *ip variable: ";

  cout << *ip << endl;

  return 0;

}

The Output:

Value of var variable: 20

Address stored in ip variable: 0xbfc601ac

Value of *ip variable: 20

* © 2012 TutorialsPoint.COM, C++ Pointer, This is an Example Program taken from the website http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm

This is a guest post by Robin Mckenzie. Robin Mckenzie has been writing articles related to Technology News, Gadget Reviews and How To’s. He also does guest posting for bundles.telcoservicesgroup.net, a site that offers great savings and up-to-date information on consumers broadband internet and cable. With service from Centurylink, you get top-speed internet, excellent value, and award-winning security.


Spyros