Compare Variable Addresses
Create a simple program that demonstrates how to use the & operator to get the memory addresses of two variables. The program should then coreturn 0;mpare these memory addresses and determine which variable has a higher memory address.
Hint
Use the & operator to get the address of a variable.
Answer
#include <iostream>
int main() {
int var1 = 42;
int var2 = 84;
std::cout << "Memory address of var1: " << &var1 << std::endl;
std::cout << "Memory address of var2: " << &var2 << std::endl;
// Compare the memory addresses and determine which variable has the higher memory address
if (&var1 > &var2) {
std::cout << "var1 has a higher memory address than var2." << std::endl;
} else {
std::cout << "var2 has a higher memory address than var1." << std::endl;
}
}