Pointers in CPLUSPLUS


image credit: www-numi.fnal.gov

Pointers in CPLUSPLUS
is the most sought after topic in C++. There are so many reasons of using pointers on C++ such as:

1.Most popular topics of C++ are pointers. They are purely conceptual based when deployed in the applications such s memory management.
2.A pointer does not point to the value in a variable. What is does, it points to the memory address in which our variable is stored.This is known as pointing value by reference .
3.Pointers are the assets of C++ programing.It’s mechanism of holding the address value rather than the direct value makes it unique and beautiful
4.By definition, pass by value is a way to copy the value of the variable to memory without being concerned about its location in huge memory pool.
5. Pointers can hold the base address of arrays, with that base address, a whole array elements can be accessed
6.Dynamic Memory Allocation is the asset of C++ pointers. With the use of pointers,we can dynamically allocate memory which i upto the choice
7. Bjarne Stroustrup (creator of c++ ) states;
“Games: Doom III engine. Sierra On-line: Birthright, Hellfire, Football Pro, Bullrider I & II, Trophy Bear, Kings Quest, Antara, Hoyle Card games suite, SWAT, and too many others to list… Blizzard: StarCraft, StarCraft: Brood War, Diablo I, Diablo II: Lord of Destruction, Warcraft III, World of Warcraft. Quicksilver: Shanghai Second Dynasty, Shanghai Mah Jongg Essentials, Starfleet Command, Invictus, PBS’s Heritage: Civilization and the Jews, Master of Orion III, CS-XII. Microsoft: all games. EA: video game engine. Byond: a “world” development platform..”
I’m certain there are many others as well. An infamous game called Trespasser, a dinosaur game, was written in c++ as well, I base this simply on knowing the game executable well and the game’s functions are definitely c++.
above paragraph has direct credit to:https://answers.yahoo.com/question/index?qid=20070513141231AAKEK10

Pointer declaration and example in program

type *var-name;
int *i; // integer pointer
double *d; // double pointer
float *f; // float pointer
char *ch // character pointer

The method of declaration is quite easy and straight forward as mentioned. Let us take a working example:

#include “stdafx.h”
#include <iostream>

using namespace std;

int main ()
// variable initialization
int var = 40;
// pointer of type int is declared
int *ptr;

// pointing the address of var variable. & shows that ome thing is mentioned by its address not value
ptr = &var;

cout << “Variable”;
cout << var << endl;

cout << “Address held by *ptr: “;
cout << ptr << endl;

// giving the value of a variable by using * notation
cout << “Value of *ptr variable: “;
cout << *ptr << endl;

return 0;

#include “stdafx.h”
#include <iostream>

using namespace std;

int main ()
// intager
int i;
int *p;
// pointer to pointer
int **pp;
//i initialize with 1200
i = 1200;

// pointer p has the address of i
p = &i;

// pointer pp holding the address of pointer p
pp = &p;

// Now the magic begins
cout << “i :” << i << endl;
cout << “*p :” << *p << endl;
cout << “**pp :” << **pp << endl;

return 0;


Pointer to pointer and array operation and iterating through the elements

#include “stdafx.h”
#include <iostream<

using namespace std;

int main ()
// an array with 5 elements.
int Marks[5] = {100,80,80,85,92};
int *ptr;
int iterator=0;
// ptr is holding the base address or addres at zeroeth index of any array
ptr = Marks;

// Show stored elements
cout << “Traversing with *ptr ” << endl;
cout << “*(ptr + ” << iterator << “) : “;
cout << *(ptr + iterator) << endl;

return 0;


C++ pointers features

C has one group of pointers and C++ has three groups.

In C you can have a pointer to any type, including pointers to pointers. So given any type you can have a pointer type to it.

typedef T* PointerTypeToT;

In C++ you have the same simple pointer types you have in C, plus you have a special pointer type called a reference.

typedef T& ReferenceTypeToT;

A reference type is a pointer that uses value type syntax to access it and has to be initialised on declaration. It is more difficult (but not impossible) to assign NULL values to a reference type.

Finally C++ has member pointers:

typedef T S::* PointerToTypeTInS;

This is a pointer that points into a given class. If a class has several member int members a, b and c, then this allows you to create a pointer that points to one of those members for any given instance of the class.

Both C and C++ consider functions to be just special kinds of objects, so all of the above applies to function pointers.


Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.