what is pointer in C++?
pointer in C++
A pointer is a variable that is used to store a memory address. The reference operator is used to access the memory address of a variable and store it in a pointer.
in simple word we say that a pointer is a variable that is store the value and address of another variable in c++ program.
pointer is used in program to access memory and manipulate addresses. pointer is most important and powerful feature in c++ program. pointer have a data type to store the value and address the another variable value.
it allow the user to access the value in the memory . the pointer variable refers to the memory using its address. A program may declare many more variable . The variable declaration reserves a specific amount of space in memory. they can access and manipulate data in computer directly.
example.
A pointer is integer type can hold the address of another integer variable as similarly a pointer of character type hold to store the address of another character type variable.
A pointer can also create and manipulate dynamic data structures.
Advantage of pointer.
some advantage of using pointer variable are as follow.
- It can access the memory address directly.
- It can save memory.
- It runs faster as it does not duplicate data.
pointer Declaration.
The method of declaring a pointer is same as declaring a simple variable. An asterisk(*)
is used in the declaration that indicate that the variable is a pointer variable.
the declaration of pointer take the following syntax like example
the declaration of pointer is as follow.
DataType*variablname;
in above example
Data Type It is the type of variable pointed by the pointer variable.
* it is indicates that the variable is a pointer variable .
variable name it is used the name of the pointer variable.
example of declaration pointer with program in c++
write a program which input number is a integer variable . it store the address of the variable in a pointer and then display the value and address of the variable.
let start to develop pointer program
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
int*ptr;
cout<<"Enter an integer:";
cin>>n;
ptr=&n;
cout<<"The value of n:"<<n<<endl;
cout<<"The address of n;"<<n<<endl;
getch()
}
output of the program
Enter an integer: 22
Enter value of n: 22
The address of n: 0x8fbdeee3