// 使用new动态分配存储空间
#include<iostream>
using std::cout;
int main()
{
// 第1种方式
int *a=new int;
*a=1;
cout<<"使用第一种方式进行动态分配存储空间的结果为:\n"
<<"*a= "<<*a<<std::endl;
delete a; // 释放动态存储空间
// 第2种方式
int *b=new int(2);
cout<<"使用第一种方式进行动态分配存储空间的结果为:\n"
<<"*b= "<<*b<<std::endl;
delete b; // 释放动态存储空间
// 第3种方式
int *c;
c=new int(3);
cout<<"使用第一种方式进行动态分配存储空间的结果为:\n"
<<"*c= "<<*c<<std::endl;
delete c; // 释放动态存储空间
// 动态创建数组
float *d=new float [3];
d[0]=3;
d[1]=6;
d[2]=8;
cout<<"d[0]= "<<d[0]<<std::endl;
d=d+1; //数组名和指针之间的根本区别
cout<<"d[0]= "<<d[0]<<std::endl;
d=d-1;
cout<<"d[0]= "<<d[0]<<std::endl;
delete [] d; // 释放动态存储空间
return 0;
}
#include<iostream>
using std::cout;
int main()
{
// 第1种方式
int *a=new int;
*a=1;
cout<<"使用第一种方式进行动态分配存储空间的结果为:\n"
<<"*a= "<<*a<<std::endl;
delete a; // 释放动态存储空间
// 第2种方式
int *b=new int(2);
cout<<"使用第一种方式进行动态分配存储空间的结果为:\n"
<<"*b= "<<*b<<std::endl;
delete b; // 释放动态存储空间
// 第3种方式
int *c;
c=new int(3);
cout<<"使用第一种方式进行动态分配存储空间的结果为:\n"
<<"*c= "<<*c<<std::endl;
delete c; // 释放动态存储空间
// 动态创建数组
float *d=new float [3];
d[0]=3;
d[1]=6;
d[2]=8;
cout<<"d[0]= "<<d[0]<<std::endl;
d=d+1; //数组名和指针之间的根本区别
cout<<"d[0]= "<<d[0]<<std::endl;
d=d-1;
cout<<"d[0]= "<<d[0]<<std::endl;
delete [] d; // 释放动态存储空间
return 0;
}