This article provides a guide to finding a variable’s memory address in C++.
How to Determine a Variable’s Memory Address in C++
In C++, there are two methods for determining the memory address of a variable:
Method 1: Find the Variable’s Memory Address Using ‘address of’ & Operator
When we are required to determine the memory address of a variable, we can utilize the “address of operator” (&), which returns the variable’s address. To display the address of a variable, use ‘&’ along with the variable name.
Let us use an example program to demonstrate this:
using namespace std;
int main()
{
int num1;
float num2;
string str;
cout<<"Please enter an integer value to find its memory address: ";
cin>>num1;
cout<<"Entered number is: "<<num1<<endl;
cout<<"Address of "<<num1<<" is: "<<&num1<<endl;
cout<<"Please enter a decimal value to find its memory address: ";
cin>>num2;
cout<<"Entered number is :"<<num2<<endl;
cout<<"Address of "<<num2<<" is :"<<&num2<<endl;
cout<<"Please enter a string to find its memory address: ";
cin>>str;
for( int i=0; i<str.length(); ++i )
cout<<"Address of "<<str[i]<<" is :"<<(void*)&str[i]<<endl;
return 0;
}
Our variable labels in the above program are num1, num2, and str, with data types of int, float, and string, respectively. The ‘&’ operator is used with the variable name to find its memory address. It should be noted that the output that provides the variable’s address is a randomly generated number.
Method 2: Find a Variable’s Memory Address Using a Pointer
In C++, variable addresses can also be obtained using pointers. A variable that holds another variable’s address is referred to as a pointer. A pointer serves like any other variable that must be specified before it can be utilized. A pointer is defined in C++ with the ‘*’ operator, sometimes known as the asterisk operator.
Let’s look at an example program to find memory address using pointer:
#include <string>
using namespace std;
int main() {
int num1;
int *ptr_num1 = &num1;
float num2;
float *ptr_num2 = &num2;
string str;
cout << "Please enter an integer value to find its memory address: ";
cin >> num1;
cout << "Entered number is: " << num1 << endl;
cout << "Address of " << num1 << " is: " << ptr_num1 << endl;
cout << "Please enter a decimal value to find its memory address: ";
cin >> num2;
cout << "Entered number is: " << num2 << endl;
cout << "Address of " << num2 << " is: " << ptr_num2 << endl;
cout << "Please enter a string to find its memory address: ";
cin >> str;
for (int i = 0; i < str.length(); i++) {
cout << "Address of " << str[i] << " is: " << (void*)&str[i] << endl;
}
return 0;
}
The variable labels in this program are num1, num2, and str, with data types of int, float, and string, respectively. The variable address is determined using the pointer along with the variable name. It should be noted that the output that provides the variable’s address is a randomly generated number.
Conclusion
When we declare a variable of any data type in C++, it is assigned a memory location by the operating system. This memory location is an arbitrary number that cannot be negative and is known as the variable’s address. Knowing the memory address of a variable can be useful in certain programming scenarios. In this article, we illustrated two methods, the address of operator(&) and pointer variables, to determine the memory address of a variable in C++ along with simple examples.