- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Pointers
A pointer is a variable that holds the memory address, usually the location of another variable in the memory. The pointer are one of the C++'s most useful and strongest features.The correct understanding and use of pointers is critical to successful programming in C++.
Why Pointers are so Important ?
Here are the reasons for the importance of pointers in C++.
- Pointers provides the means by which the memory location of a variable can be directly accessed and hence can be manipulated in the way as required.
- Pointers supports C++'s dynamic allocation routines.
- Pointers can improve the efficiency of certain routines.
Pointers are strongest as well as dangerous
Pointers are one of the strongest and also one of the dangerous and vulnerable features of C++. For example, uninitialized, or wild pointers can use your system to crash. Perhaps worse, it is easy to use pointers incorrectly, causing bugs that are very difficult to find. So, it becomes very much necessary to understand and grasp this important concept in order to exploit its power and use without creating problems in a program.
Ideas behind Pointers
The ideas behind pointers are not complicated. Here's the first key concept: Every byte in the computer's memory has an address. Addresses are numbers, just as your house numbers. Moreover, the address numbers start at 0 and go up from there 1, 2, 3, and so on. For example, if you have 640 KB memory i.e., 640 × 1024 bytes i.e., 655360 bytes of memory, then the first byte will be having address 0, second as 1, third as 2, and so on. The highest address will be 655,359. A variable storing a memory address is called a pointer as it points to a specific memory location whose address it is storing under its name.
C++ Pointer Declaration
Here is the general form to declare a pointer in C++. Pointer variables are declared just like the normal variables except for the addition of the unary * character:
type *var_name;
Here, type is any valid C++ data type and var_name is the name of the pointer variable. Here are some example of pointer declaration in C++:
int *iptr; char *cptr; float fptr;
The above statements, creates three pointers. A integer pointer, iptr, and a character pointer, cptr, and a floating-point pointer, fptr.
C++ Pointer Initialization
Two special operators are used with pointers that is, * and &. The & operator is a unary operator that returns the memory address of its operand. Let's look at these statements:
int num = 20; // declares an int variable named num int *iptr; // declares an int pointer named iptr iptr = # // this stores the memory address of num into iptr
Let's look at below statements to understand the concept of pointers in C++:
int num = 20; int *iptr; iptr = # int val = *iptr;
From the above statements, first a variable num of type int is created and initialized with value of 20. Now a pointer variable iptr of type int is declared. And the address of the variable num is initialized to the pointer iptr. Now the value present at the address num is initialized to the variable val of type int which is 20 using the operator * (also called as value at address operator).
C++ Pointers Example
Here are some examples demonstrating the concept of pointers in C++ practically:
/* C++ Pointers - Example Program of C++ Pointers */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int num=20; int val; int *iptr; iptr = # val = *iptr; cout<<"Statements:\n"; cout<<"\tint num=20;\n\tint val;\n\tint *ptr;"; cout<<"\n\tiptr = #\n\tval = *iptr\n\n"; cout<<"Output:\n"; cout<<"\tnum = "<<num<<"\n\tval = "<<val; getch(); }
Here is the sample output of the above C++ program:

Here is one more C++ pointer example:
/* C++ Pointers - Example Program of C++ Pointers */ #include<iostream.h> #include<conio.h> void main() { clrscr(); float a = 5.999; float *b, *c; b = &a; c = b; cout<<"Statements:\n"; cout<<"\tfloat a = 5.999;\n\tfloat *b, *c;\n"; cout<<"\tb = &a\n\tc = b\n\n"; cout<<"Output:\n\t"; cout<<"a = "<<a<<"\n\t"; cout<<"*(&a) = "<<*(&a)<<"\n\t"; cout<<"*b = "<<*b<<"\n\t"; cout<<"*c = "<<*c<<"\n"; getch(); }
Here is the sample output of this C++ program:

Here is another type of program demonstrating pointers in C++
/* C++ Pointers - Example Program of C++ Pointers */ #include<iostream.h> #include<conio.h> void check(int, int *); void main() { clrscr(); int a = 6, b = -4; cout<<"Before:\t"; cout<<a<<"\t"<<b<<"\n\n"; check(a, &b); cout<<"After:\t"; cout<<a<<"\t"<<b; getch(); } void check(int x, int *y) { x = x*x; *y = *y * *y; cout<<"In:\t"; cout<<x<<"\t"<<*y<<"\n\n"; }
Below is the sample run of the above C++ program:

Let's take another type of program also demonstrating pointers in C++
/* C++ Pointers - Example Program of C++ Pointers */ #include<iostream.h> #include<conio.h> void fun(int, int *); void main() { clrscr(); int arr[5] = {2, 4, 6, 8, 10}; int i, b=5; for(i=0; i<5; i++) { fun(arr[i], &b); cout<<arr[i]<<"\t"<<b<<"\n"; } getch(); } void fun(int x, int *y) { x = *(y) += 2; }
Below is the sample output of the above C++ program:

Let's take one more type of program of C++ pointers:
/* C++ Pointers - Example Program of C++ Pointers */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int arr[] = {4, 6, 10, 12}; int *ptr; int i; ptr = arr; for(i=0; i<3; i++) { cout<<*ptr<<" -> "; ptr++; } cout<<"\n"; for(i=0; i<4; i++) { (*ptr)*=3; --ptr; } for(i=0; i<4; i++) { cout<<arr[i]<<" -> "; } getch(); }
Below is the sample output of the above C++ program:

Another type of C++ pointer program is given here:
/* C++ Pointers - Example Program of C++ Pointers */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int arra[] = {11, 22, 33, 44, 55}; int a; int *ptra; ptra = arra; a = *ptra++; cout<<"*ptra = "<<*ptra<<"\n"; cout<<"a = "<<a<<"\n\n"; int arrb[] = {11, 22, 33, 44, 55}; int b; int *ptrb; ptrb = arrb; b = (*ptrb)++; cout<<"*ptrb = "<<*ptrb<<"\n"; cout<<"b = "<<b<<"\n\n"; int arrc[] = {11, 22, 33, 44, 55}; int c; int *ptrc; ptrc = arrc; c = *++ptrc; cout<<"*ptrc = "<<*ptrc<<"\n"; cout<<"c = "<<c<<"\n\n"; getch(); }
This is the output of the above C++ program:

You will learn all about pointers, one by one, divided in these separate chapters. These chapters provides detail informations about pointers:
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declaration and Initialization of Pointers
- C++ Dynamic Allocation Operators
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
« Previous Tutorial Next Tutorial »