Answer to Question #220202 in C++ for suk

Question #220202

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,


1
Expert's answer
2021-07-25T08:35:41-0400


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.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS