今天Candy看到了关于C++的一个语法!
int* p = new int[2];
Candy的第一反应是,这是一个指针数组!
然后等到在群里问了一大牛们后,得到结果:这是一个指针(数组)!
奇怪!
Candy(试图)用正常的语法来解释: " 用new 申请了一个int类型宽度为[2]一个数组指针! "
然后我开始写代码试图对其进行赋值!
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int* p = new int[2];
p[0] = 1;
p[1] = 2;
cout<<p[0]<<endl<<p[1]<<endl;
return 0;
}
结果:
1
2
Process returned 0 (0x0) execution time : 0.078 s
Press any key to continue.
成功!
然后我又试了其他几种语法!
int* p = new int[2];
等同于
int* p = new int;
等同于
int p[2];