Return pointer and array
02:43 08 Feb 2026
#include 
using namespace std;

int* func2() {

    //创建10整型数据的数组,在堆区
    int* p = new int[10];  //10代表数组有10个元素
    return p;
}

void test02() {

    int* arr = func2();

    for (int i = 0; i < 10; i++) {

        arr[i] = 100 + i;
        cout << arr[i] << endl;
    }
}

After calling the func2() function, the returned pointer type is assigned to arr. Shouldn't arr be a pointer? Why can we use arr[i] directly to print it out?

c++