New 一个数据类型 返回这个数据类型的指针 new创建的堆区的数据,如果要释放需要用 关键字delete 释放数组的时候需要加中括号
#include <iostream>
#include <ctime>
#include <string>//c++中字符串需要添加这个头文件
using namespace std;
int *fun()
{
int *p = new int(88);
return p;
}
int main()
{
int *s = fun();
cout << *s << endl;
delete s;
cout << *s << endl;
system("pause");
return 0;
}
#include <iostream>
#include <ctime>
#include <string>//c++中字符串需要添加这个头文件
using namespace std;
int *fun()
{
int *p = new int[10];
for (int k = 0; k < 10; k++)
{
*(p + k) = k * k;
}
return p;
}
int main()
{
int *s = fun();
for (int k = 0; k < 10; k++)
{
cout << *(s + k) << " ";
}
cout << endl;
delete[] s;
system("pause");
return 0;
}