assign value to a pointer
#include <iostream>
using namespace std;
int main() {
int x = 10; // Step 1: Define an integer variable x and assign it the value 10
int *ptr; // Step 2: Declare an integer pointer variable ptr
ptr = &x; // Step 3: Assign the address of variable x to the pointer ptr
cout << "The value of x: " << x << endl; // Step 4: Output the value of x
cout << "The address of x: " << &x << endl; // Step 5: Output the address of x
cout << "The value pointed to by ptr: " << *ptr << endl; // Step 6: Output the value pointed to by ptr
return 0;
}
Explanation:
1. Define an integer variable x
and assign it the value 10
.
2. Declare an integer pointer variable ptr
.
3. Assign the address of variable x
to the pointer ptr
.
4. Output the value of x
.
5. Output the address of x
.
6. Output the value pointed to by ptr
, which is the value of x
.