Table of Contents
Pointer in C++ programming language
In this tutorial, we will discuss the concept of Pointer in C++ programming language
Knowledge area
- What is the pointer in C++
- Diclaration & initiation of pointer
- find variable address & pointer
- Advantages and disadvantages of pointer
pointer
- the pointer is one of the features of C++ language
- The pointer is a very fundamental and important concept program in C++ Language.
- The pointer is a one of the variables is basically same as other variables in C++. but which can store piece of data
- The normal variable which store a value such as an int, a double . a char.
- A pointer store a memory address is similar to an array but array is statically pointer is dynamical. dynamical means we can increased or decreased memory allocation to my need.
- So pointer is a special type of variable that contains just the address of another variable. it is called pointer
what is memory unit
As we know, a memory unit is a collection of storage which cans store information
Each memory cell is given an identification number, it’s called address
if a memory can store n- information it’s address ranges from 0 to n-1.
Variable and memory address of variable in C++
A pointer is a data type that point to another value stored in memory
var is used to get value of variable and &var is used to get address of the variable in memory location
Declaring and initialishing pointers
All pointers variable must be declared before it is used in the program like other variables. The general form is,
Syntex
var – it is a variable on you program in C
&var – it is a address of your veriable in the memory location
*var – it is a pointer of your memory location
data type* var; // Dicare a pointer variable called var as pointer of type
data type *var;
data type * var;
– data type means such as int, float double etc
* (sign) – to identify the variable as a pointer
*ptr – this is a pointer variable
var- this is a normal variable
Here, ptr is a pointer which hold the address of variable var
Example :
int *a; // Dicare a pointer variable called a as pointing to an int
// it contain an address holds an int value
or
char* a; // Dicare a pointer variable called a as pointing to a char
// it contain an address holds a char value
or
double * a; // Dicare a pointer variable called a as pointer a double
// it contain an address holds a double value
Program 1
#include <iostream>
using namespace std;
int main()
{
int var1=10; // diclare a int variable
char var2[10]=”kamal”; // diclare a char variable
float var3=6.47; // diclare a float variable
cout << var1 << endl; //display value of var1
cout<<var2<<endl; //display value of val2
cout <<var3<< endl; //display value of var3
cout <<“………….”<< endl;
cout << &var1 << endl; //display address of var1
cout<<&var2<<endl; //display address of var2
cout <<&var3<< endl; //display address of var3
return 0;
}
At above program, we diclare three type of variable var1, var2, var3.
we can print variables ‘var’ and variable addresses use ‘&var’
Program 2
#include <iostream>
using namespace std;
int main()
{
int value; // diclare a normal variable
int *ptr; // declare a pointer variable
value=90; // assign a value to normal variable
ptr=&value; //&value is address of value assign
//to pointer value(ptr)
cout << value << endl; //display of value
cout<<&value<<endl; //display value of address
cout << ptr << endl; //display value of address
cout << *ptr << endl; //display of value
return 0;
}
Advantages of pointers
pointer increased the execution speed of the program
Pointers reduced the length and complexity of a program
Pointer enable a variable that is defined outside function
pointer used to pass information back and forth between a function and it’s reference point
Disadvantages of pointers in C language
Uninitialized pointersmight cause segmentation fault
pointers are slower than other variables
if sufficient memory is not available during runtime for the storage of pointers the program may crash
Related Post
Pointers in C Programming language
Pointer to pointer in C Programming Language
Pointer to pointer in C++ Programming Language
Pointers with one d array in C language
Structure with pointers in C Language
Structure with pointers in C++ Language