C++ Assign Pointer to Pointer
<pre><code>#include &lt;iostream&gt;
using namespace std;

void fn(int* input_ptr) {
    int *ptr2 = input_ptr;
    cout &lt;&lt; "pointer 2 is " &lt;&lt; *ptr2 &lt;&lt; endl;
}

int main() {
    int *ptr1;
    *ptr1 = 7;
    fn(ptr1);
}
</code></pre>

<p>This example works, as I pass a pointer to the function, and assign that to a temporary pointer inside the function, the result shows pointer 2 is also 7.
However,</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;

int main() {
    int *ptr1;
    *ptr1 = 7;
    int *ptr2;
    *ptr2 = ptr1; // or I have also tried *ptr2 = *ptr1
    cout &lt;&lt; "pointer 2 is " &lt;&lt; *ptr2 &lt;&lt; endl;
}
</code></pre>

<p>The same step does not work in the main function. I know you can use the address ptr2 = ptr1 without asterisk and it will work.</p>

<p>However, in the first example, I can directly assign a pointer as a function parameter to a new pointer with asterisk (or called dereferecing?), but in the second example I cannot do that in main function.</p>

<p>Could anyone help with this question? Appreciate your time.</p>