Table of Contents
In this tutorial, we will discuss the concept of Pointer in C++ programming language
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.