const与基本数据类型

906 阅读1分钟

const修饰符

const与基本数据类型

const int x=3; //常量

变量名 存储地址 存储内容
x &x 3(不可更改的值)

const与指针类型

对号 代表等价

  • const int *p =NULL ;
  • int const *p=NuLL;
  • int* const p=NULL;
  • const int* const p=NULL;
  • int const* const p=NULL;

例子:

int x=3; const int *p=&x;

p=&y;正确 *p=4;错误

因为const修饰的是*p而不是p,所以p可以更改值,而 *p不能更改值。

变量名 存储地址 存储内容
x &x 3
p &p &x

const修饰符修饰谁,谁就不能变。

内存的申请和释放

申请内存运算符

关键词 new detle