Address of
Variable in C++ Program
Addresses
of variables are very important in C++. These are used to refer indirectly a
variable. The concept of using addresses in C++ is not quite common but it is
used, especially in the pointers this concept is widely used. In this article
we will not discuss the address of variables relationship with pointers but we
will simply focus on the address of variable.
How to
Access Address of Variable in C++
To
access the address of variable, if you are familiar with some other programming
language, especially C. Then the ampersand operator & will come in your
mind, if not don’t worry. We tell you that, In programming languages &
operator is used to access the address of variables.
Importance
of Address of Variable in C++
Suppose
that you know a man name Ali, than knowing the name of Ali is not sufficient if
you want to access or met the Ali. You probably need the address or some other
contact detail of Ali to reach Ali.
Same
in programming if know the name of a variable it is not sufficient. It will be
used for basic activities like printing or performing calculations. To apply
the advanced technique you will need to access the variable using the address.
One
thing to be noted that these advanced technique are in pointers.
We
are not covering pointer, we will simply know how to access the address of
variables using the ampersand operator &.
So
let’s start coding
C++ Program
to print Address of Variable
Code
#include<iostream>
using
namespace std;
int
main()
{
int var1;
int var2;
int var3;
cout <<"Enter three
Numbers"<<endl;
cin>>var1>>var2>>var3;
cout<<"Numbers
are"<<endl;
cout<<var1<<endl<<var2<<endl<<var3<<endl;
cout<<" Addresses of
Variables are"<<endl;
cout<<&var1<<endl<<&var2<<endl<<&var3<<endl;
return 0;
}
Output
You
can see on this black screen how the address of variables in the memory of
computer. These addresses are only understandable by computer and difficult for
human to understand.
Address of Variables Program in C++ Output
What we do
here
Here
in this program we have declared three variables var1, var2 and var3. Ask the
user to enter the values of these variables using the cout statement. Then take
the values form user using the cin statement and then print the values of these
variables using cout statement. At last we have printed the addresses of
variables using cout statement by accessing them by ampersand/ address
operator.
Thanks
to take help from SofOSet and its quality material.
Post a Comment
Feel Free to Comment Us