&& c++
#include <iostream>
using namespace std;
int main() {
int num1 = 10;
int num2 = 20;
int ptr1, ptr2;
ptr1 = &num1;
ptr2 = &num2;
cout << "Value of num1: " << num1 << endl;
cout << "Value of num2: " << num2 << endl;
cout << "Address of num1: " << &num1 << endl;
cout << "Address of num2: " << &num2 << endl;
cout << "Value of ptr1: " << *ptr1 << endl;
cout << "Value of ptr2: " << *ptr2 << endl;
return 0;
}
Explanation:
#include <iostream>
: Includes the input-output stream header file that allows input and output operations.using namespace std;
: Enables the usage of entities in the standard C++ library without explicitly qualifying their names withstd::
.int main() { ... }
: The main function where the execution of the program begins.int num1 = 10;
andint num2 = 20;
: Declaration and initialization of two integer variablesnum1
andnum2
with values 10 and 20, respectively.int ptr1, ptr2;
: Declaration of two integer pointersptr1
andptr2
.ptr1 = &num1;
andptr2 = &num2;
: Assigns the memory addresses ofnum1
andnum2
to the pointersptr1
andptr2
, respectively.cout << "Value of num1: " << num1 << endl;
andcout << "Value of num2: " << num2 << endl;
: Outputs the values ofnum1
andnum2
to the console.cout << "Address of num1: " << &num1 << endl;
andcout << "Address of num2: " << &num2 << endl;
: Outputs the memory addresses ofnum1
andnum2
to the console.cout << "Value of ptr1: " << ptr1 << endl;
andcout << "Value of ptr2: " << ptr2 << endl;
: Outputs the values pointed to byptr1
andptr2
(which are the values ofnum1
andnum2
, respectively) to the console using dereferencing.return 0;
: Indicates successful termination of themain()
function.