You are given this snippet:
int main(){
int m = 10, n = 20;
cout << &m << endl; // Whats is &m?
int* ptr1 = &m; // Explain this line.
cout << ptr1 << endl; // What is ptr1?
cout << *ptr1 << endl; // What is *ptr?
int* ptr2 = ptr1; // Explain this line.
ptr1 = &n; // Explain this line.
cout << &ptr1 << endl; // What is &ptr?
return 0;
}
Please answer the questions in comments,
What is &m? &m gives the memory address to the integer variable m.
int* ptr1 = &m; ptr1 stores the address of variable m.
ptr1 prints content of ptr (address of m).
* ptr1 dereference ptr by *ptr (= m)
int* ptr2 = ptr1;ptr2 points to the same address (of m)
ptr1 = &n; ptr1 now points to n
&ptr1 output 19.
Comments
Leave a comment