常量与指针:
const int* p1 = &i;
int const* p2 = &i;
int* const p3 = &i;
const int* const p4 = &i;
*p1 = 1; // compile error
p1 = NULL; // ok
*p2 = 2; // compile error
p2 = NULL; // ok
*p3 = 3; // ok
p3 = NULL; // compile error
*p4 = 4; // compile error
p4 = NULL; // compile error const int* p1 = &i;
int const* p2 = &i;
int* const p3 = &i;
const int* const p4 = &i;
*p1 = 1; // compile error
p1 = NULL; // ok
*p2 = 2; // compile error
p2 = NULL; // ok
*p3 = 3; // ok
p3 = NULL; // compile error
*p4 = 4; // compile error
p4 = NULL; // compile error
记住const 在*左边的时候,代表指针指向的数据是常量 不能用*p 去改变值的大小;
记住const 在*右边的时候,代表指针本身是常量 不能用p 去改变指针的大小;
左 shu 右 zhi;
数组地址与数组名的概念:
首先,明白 程序中的 地址 是一般包括 地址和长度的,这两者长度是不一样的额;
数组名 可以简单的看作 是一个 常量指针 , 不能出现在左边 只能作为右值使用;
特殊情况下,不可以看作是常量指针:
数组名 作为sizeof 操作符的参数;
数组名用来去地址的时候&;