codescracker


c++

C++ Pointers



« Previous Tutorial Next Tutorial »


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 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 = &num;
	val = *iptr;
	cout<<"Statements:\n";
	cout<<"\tint num=20;\n\tint val;\n\tint *ptr;";
	cout<<"\n\tiptr = &num;\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:

c++ pointers

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:

pointers in c++

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:

c++ pointer

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:

c++ pointer example

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:

pointer in c++

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:

c++ pointer program

You will learn all about pointers, one by one, divided in these separate chapters. These chapters provides detail informations about pointers:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test