This C++ Tutorial Covers
What is a Pointer in C++ Programming?
A pointer is a variable which stores or points to memory address of a variable. Pointer can access indirectly the value of a variable using its address. Both pointer and variable will be of same data type.
Declaration of a Pointer
Like other variables pointer declares in the same way. First write the data type then its name with asterisk before pointer's variable name.
Declaration Syntax
int *ptr;
where ptr is a pointer of integer and will point to an integer address variable normally we read it from right to left as ptr is a pointer to an integer value
Its is recommend that while declaring a pointer use the short word _ptr after variable name which will increase the readability of program.
Initialization of a Pointer
Like other variables pointers can be initialize at the time of declaration or after it using assignment method.
It initialize with value 0 or NULL and corresponding data type.
For example
int *ptr, a=5;
ptr=NULL; and ptr=0; both have same meaning
A pointer which points to a NULL value called as a NULL Pointer. The point to be noted here 0 is an interger value but pointer stores address only if we do int *ptr=7; or any other integer value then error occurred.
Students should Remember that 0 is the only integer value by which
Pointer with Unary Operator &
To get the address the unary operator & known as address operator is used.
for example
int *ptr;
int x=5;
ptr=&x; // assigns the address of variable x to pointer ptr
we said that ptr is a pointer pointing to memory address of variable x and indirectly pointer can access the value of variable x.
Asterisk * Operator With Pointer
The * operator is usually know as de-refrence operator used to get the value of variable using pointer.
For Example
To get Value of a variable
int *ptr; // Don't confuse here it is pointer declaration
x=10;
ptr=&x;
cout<<ptr; // will print the address of x
cout<<*ptr; // will print the value of x = 10;
Using on left side
*ptr=15;
cout<<*ptr; // now will print 15;
now the memory address of x value has changed replaced by 15
To take input
cin>> *ptr; // it will take input
now the memory address of x value has changed by what value is entered
Relation between * and address operator &
Both operators cancel the each other value and the value of variable is provided as result.
For example
int *ptr, a=5;
ptr=&a;
Let address of a= 0X100; so ptr=0X100;
cout<<*ptr<<endl; // 5
cout<<ptr; // 0X100
cout<<*&ptr<<endl; // 0X100
cout<<&*ptr<<endl; // 0X100
cout<<*(&ptr)<<endl; // 0X100
cout<<&(*ptr)<<endl; // 0X100
Errors while working with pointers
- Pointers Syntax, declaration, initialization
- Pointers with address (&) and asterisk (*) operator
- Errors may found while working with pointers
- Basic example pointers C++ program
- Relation of Asterisk and address operator
What is a Pointer in C++ Programming?
A pointer is a variable which stores or points to memory address of a variable. Pointer can access indirectly the value of a variable using its address. Both pointer and variable will be of same data type.
Declaration of a Pointer
Like other variables pointer declares in the same way. First write the data type then its name with asterisk before pointer's variable name.
Declaration Syntax
int *ptr;
where ptr is a pointer of integer and will point to an integer address variable normally we read it from right to left as ptr is a pointer to an integer value
Its is recommend that while declaring a pointer use the short word _ptr after variable name which will increase the readability of program.
Initialization of a Pointer
Like other variables pointers can be initialize at the time of declaration or after it using assignment method.
It initialize with value 0 or NULL and corresponding data type.
For example
int *ptr, a=5;
ptr=NULL; and ptr=0; both have same meaning
A pointer which points to a NULL value called as a NULL Pointer. The point to be noted here 0 is an interger value but pointer stores address only if we do int *ptr=7; or any other integer value then error occurred.
Students should Remember that 0 is the only integer value by which
Pointer with Unary Operator &
To get the address the unary operator & known as address operator is used.
for example
int *ptr;
int x=5;
ptr=&x; // assigns the address of variable x to pointer ptr
we said that ptr is a pointer pointing to memory address of variable x and indirectly pointer can access the value of variable x.
Asterisk * Operator With Pointer
The * operator is usually know as de-refrence operator used to get the value of variable using pointer.
For Example
To get Value of a variable
int *ptr; // Don't confuse here it is pointer declaration
x=10;
ptr=&x;
cout<<ptr; // will print the address of x
cout<<*ptr; // will print the value of x = 10;
Using on left side
*ptr=15;
cout<<*ptr; // now will print 15;
now the memory address of x value has changed replaced by 15
To take input
cin>> *ptr; // it will take input
now the memory address of x value has changed by what value is entered
Relation between * and address operator &
Both operators cancel the each other value and the value of variable is provided as result.
For example
int *ptr, a=5;
ptr=&a;
Let address of a= 0X100; so ptr=0X100;
cout<<*ptr<<endl; // 5
cout<<ptr; // 0X100
cout<<*&ptr<<endl; // 0X100
cout<<&*ptr<<endl; // 0X100
cout<<*(&ptr)<<endl; // 0X100
cout<<&(*ptr)<<endl; // 0X100
Errors while working with pointers
- De-referencing a NULL Pointer is an error
int *nPtr=NULL;
cout<< *nPtr; // Error - Assigning a pointer to different data type
int n; double *dPtr;
dPtr=&n; // error: cannot convert int to double - Trying to de-refrence a non pointer variable
Pointers Example program which displays different ways to deal with pointers
Compiler Used CodeBlocks C++ Compiler
read also: C++ Pointer example with pass by reference
Output
See also: Difference b/w pass by value and pass by reference with C++ example
Compiler Used CodeBlocks C++ Compiler
#include<iostream>
using namespace std;
int main()
{
int *ptr; double a=222;
int x=10;
// ptr=&a; error
ptr=&x;
cout<<"Address of ptr = "<<ptr<<"\n\n\n";
cout<<"Defrefrecned to get Value of s *ptr= " <<*ptr<<"\n\n\n";
*ptr=50;
cout<<"Changing value using *ptr=50 "<<"\n\n\n";
cout<<"Now Defrefrecned to get Value of x *ptr= "<<*ptr<<"\n\n\n";
int *nPtr=NULL;
//error
// cout<<*nPtr;
cout<<"NULL Pointer "<<&nPtr<<"\n\n\n";
cout<<"Taking Input in pointer Using cin>> *ptr : ";
cin>>*ptr;
cout<<"Input Value = "<< *ptr<<"\n\n\n";
return 0;
}
read also: C++ Pointer example with pass by reference
Output
See also: Difference b/w pass by value and pass by reference with C++ example
Recommendation: Copy the above code, edit it and examine the output of program. It will be very helpful for good learning.
See more Simple examples here of C++ programs
0 comments:
Post a Comment