'Implementing strdup() in c++ exercise from Bjarne's Book, copying char* to another char* then print out gets nothing

I'm learning c++ using the book:Programming Principles and Practice using C++ by Bjarne Stroustrup.
In Chapter 19, exercise 1
implement strdup() functions which will copy a c strings into another using only de-referencing method (not subscripting).
My copying doesn't print anything I've been look for answers for days.
Please anyone can help me?
Below is the entire code:-

#include <iostream>
using namespace std;

char* strdup(const char* q) {
    // count the size
    int n {0};
    while(q[n]) {
        ++n;
    }
    // allocate memory
    char* p = new char[n+1];
    
    // copy q into p
    while(*q) {
        *p++ = *q++;
    }
    // terminator at the end
    p[n] = 0;
    return p;
}


int main()
{
    const char* p = "welcome";
    cout << "p:" << p << endl;

    const char* q = strdup(p);
    cout << "q:" << q << endl;
    
    // check to see if q get new address
    cout << "&p:" << &p << endl;
    cout << "&q:" << &q << endl;
    
    return 0;
}


Solution 1:[1]

Replace this:

while(*q) {
    *p++ = *q++;
}

..with this:

for (int i = 0; i < n; i++) {
    p[i] = q[i];
}

Problem solved.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Solved Games