- const修饰的指针
- main函数传参
1、const修饰的指针
const:只读
用来修饰变量,使用const修饰的变量只能读,不能被修改
int a;//存放再栈区
const int a;//存放在栈区
案例:判断const修饰的变量有没有在常量区
char str[30] = “hello”;//在栈区开辟了30个字节的空间
str[0] = ‘a’;
char *p = “hello”; p存储在栈区, “hello”是字符串常量,所以在常量区
*p = ‘a’;//error 因为“hello”存储在常量区,不能被修改
char str[30] = {0};
str = “hello”; //error str是常量
char *p = NULL;
p = “hello”;
const修饰的指针:
int a = 10;
const int *p = &a;//const修饰*p,*p不能被修改
int *const p = &a;//const修饰的是 p,p的指向不能改
const int *const p = &a;*p和p都不能被修改
总结:const离谁近,就修饰谁
const离int近,修饰的是*p
const离*近,修饰的是p



