Created
December 15, 2021 13:58
-
-
Save neo7BF/6ef06dcd83bae5dcb50f94020d88624d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main () | |
{ | |
//The computer assigns to A the address of the memory cell 0x00 | |
//and stores in its data area the value 5 | |
int A = 5; | |
//With the operator & I get the address of A then the instruction | |
//print the address 0x00 on the screen | |
cout << &A << endl; | |
//I declare a variable B, pointer of type int, to which the computer | |
//assigns the memory cell to the address 0x01, then this variable is used | |
//to store the address of A. | |
int* B = &A; // the computer assigns the memoy cell to B at the address 0x01, | |
// in this cell, the address of A 0x00 is stored | |
//The following statement causes a compilation error! | |
//This commented line has been inserted to emphasize that B | |
//is a variable of type int* (int pointer) and as such can receive | |
//as a value only an address. | |
// B = 5; //Error ! B is of type int* (int pointer) is not of type int | |
//This instruction prints on the screen the data contained in the cell | |
//whose address is stored in B. Since the address of A was previously | |
//stored in B, the value 5 will be printed | |
cout << *B << endl; // print the cell data at the address | |
// contained in B i.e. 5 | |
//B is a variable of type int* (int pointer) which can store an address, | |
//since it is a variable, however, the computer also assigns an address | |
//to it. This instruction prints address of B on the screen | |
cout << &B << endl; // & gets the address of B assuming it is 0x01 | |
//In this case, instead, the data contained in B is printed, | |
//but since B is a pointer that stores only addresses | |
//and given that in it was previously stored the address | |
//of A, the data area contains is 0x00 | |
cout << B << endl; // print the data contained in B, | |
// B is a pointer that stores the address of A i.e. 0x00 | |
//The value to the cell pointed to by B is changed from 5 to 10, | |
//and since B points to the memory cell of A, it is the value of A that is changed. | |
*B = 10; // change the data of the cell pointed to by B, i.e. the value of the cell of A | |
cout << "Value value of A:" << A << endl; | |
cout << "Value contained in the cell pointed to by B:" << *B << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment