![]() |
How To Use Pointer in CPP With Example |
C Plus Plus program to create a class complex through pointers
Pointer: A Pointer may be a variable that stores the address of another variable.
It will create some things abundant easier, facilitate improve your program's potency, and even enable you to handle unlimited amounts of information.
C Pulse Pulse Pointer is employed to apportion memory dynamically at run time.
The variable may be any of the information kind like data types integer, floating, Character, double, short, etc.
Pointers need a touch of recent syntax as a result of once you have a pointer, you wish the power to each request the memory location it stores and therefore the worth keep at that memory location.
Definition: A Pointer is very most important.it is the most type of variable.it is used to store the address of another variable.
The pointer can be stored only same data types like integer data types stored in an integer value.
Pointer denotes (*) symbols with any variables at any time.it is called the pointer variable.
A pointer can be stored address using the ampersand(&) sign.it is stored address of variables.
Syntax of Pointer in C++
data_type *ptr_name;
data_type *ptr_name;
How To Use Pointer Variable in C++?
#include<iostream>
using namespace std;
int main()
{
int x=100;
int *p;
p=&x;
cout<<"Value of x:"<<x<<"\n;
cout<<"Address of x:"<<&x<<"\n;
cout<<"Value of p:"<<p<<"\n";
cout<<"Address of p:"<<&p<<"\n";
cout<<"Value of *p:"<<*p<<"\n";
}
*****OUTPUT*****
Value of x:100
Address of x:654785
Value of p:100
Address of p:654785
Value of *p:100
#include<iostream>
using namespace std;
int main()
{
int x=100;
int *p;
p=&x;
cout<<"Value of x:"<<x<<"\n;
cout<<"Address of x:"<<&x<<"\n;
cout<<"Value of p:"<<p<<"\n";
cout<<"Address of p:"<<&p<<"\n";
cout<<"Value of *p:"<<*p<<"\n";
}
*****OUTPUT*****
Value of x:100
Address of x:654785
Value of p:100
Address of p:654785
Value of *p:100
Important points to recollect regarding pointers in C++
1. Traditional variable stores the worth whereas pointer variable stores the address of the variable.
2. The content of the C++ pointer forever be a full variety address.
3.Forever C++ pointer is initialized to null, i.e. int *p = null.
4. The worth of the null pointer is zero.
5.& sign is employed to urge the address of the variable.
6.* sign is employed to urge the worth of the variable that the pointer is informed to.
7. If a pointer is appointed to NULL, it means that it's informed to data.
8. Two pointers are often ablated to grasp what percentage components are on the market between these two pointers.
9. But, Pointer addition, multiplication, division don't seem to be allowed.
10. the dimensions of any pointer are a pair of computer memory units (for a sixteen-bit compiler).
1. Traditional variable stores the worth whereas pointer variable stores the address of the variable.
2. The content of the C++ pointer forever be a full variety address.
3.Forever C++ pointer is initialized to null, i.e. int *p = null.
4. The worth of the null pointer is zero.
5.& sign is employed to urge the address of the variable.
6.* sign is employed to urge the worth of the variable that the pointer is informed to.
7. If a pointer is appointed to NULL, it means that it's informed to data.
8. Two pointers are often ablated to grasp what percentage components are on the market between these two pointers.
9. But, Pointer addition, multiplication, division don't seem to be allowed.
10. the dimensions of any pointer are a pair of computer memory units (for a sixteen-bit compiler).
This can be done at the time of variable declaration. A pointer that's appointed Null is termed a null pointer.
int *ptr = NULL;
The value of ptr is zero
Pointer Arithmetic
As you understood pointer may be an address that is a numeric value.
So you'll perform arithmetic operations on a pointer even as you'll a numeric worth.
There are four arithmetic operators which will be used on pointers: ++, --, +, and -.
How To Use Arithmetic Pointer in C++?
ptr++;
ptr--;
ptr+21;
ptr-10;
ptr++;
ptr--;
ptr+21;
ptr-10;
If a char purposer inform to deal with a hundred is incremented (ptr++) then it'll point to memory address one zero one.
Pointers vs Arrays
Pointers and arrays are powerfully connected. In fact, pointers and arrays are interchangeable in several cases.
For instance, a pointer that points to the start of the associate degree array will access that array by exploiting either pointer arithmetic or array-style compartment.
How To Pointer vs Arrays Variable?
#include<iostream>
using namespace std;
int main ()
int var[3]={10,20,30};
int *ptr;
cout<<"Value First:"<<*ptr<<"\n";
ptr++;
cout<<"Value Second:"<<*ptr<<"\n";
return 0;
}
*****OUTPUT*****
Value First:10
Value Second:20
#include<iostream>
using namespace std;
int main ()
int var[3]={10,20,30};
int *ptr;
cout<<"Value First:"<<*ptr<<"\n";
ptr++;
cout<<"Value Second:"<<*ptr<<"\n";
return 0;
}
*****OUTPUT*****
Value First:10
Value Second:20
Pointer To Pointer: A pointer to a pointer may be a type of multiple indirections or a sequence of pointers.
Normally a pointer contains the address of a variable. after we outline a pointer to a pointer, the primary pointer contains the address of the second pointer, which points to the placement that contains the particular worth.
How To Pointer vs Arrays Variable?
#include<iostream>
using namespace std;
int main ()
{
int x;
int *ptr;
int **ptr;
x = 200;
ptr = &x;
pptr = &ptr;
cout<<"Value of x :"<<x<<"\n";
cout<<"Value available at *ptr :"<<*ptr<<"\n";
cout<<"Value available at **pptr :"<<**pptr<<"\n";
return 0;
}
*****OUTPUT*****
Value of x:200
The value available at *ptr:200
The value available at **ptr:200
#include<iostream>
using namespace std;
int main ()
{
int x;
int *ptr;
int **ptr;
x = 200;
ptr = &x;
pptr = &ptr;
cout<<"Value of x :"<<x<<"\n";
cout<<"Value available at *ptr :"<<*ptr<<"\n";
cout<<"Value available at **pptr :"<<**pptr<<"\n";
return 0;
}
*****OUTPUT*****
Value of x:200
The value available at *ptr:200
The value available at **ptr:200