2019年7月13日 星期六

C 常數指標筆記

pointer to variable can not be modified, but pointer can be modified.

int a=1;
int b=3;
int const *p=&a;
const int *p2=&a;
*p=2; // failed
*p2=2; // failed
p=&b; // success
p2=&b; //success

pointer to variable can be modified, but pointer can not be modified.

int a=1;
int b=3;
int *const p=&a;
*p=2; // success
p=&b; // failed

pointer to variable can not be modified, and pointer can not be modified.

int a=1;
int b=3;
const int *const p=&a;
*p=2; // failed
p=&b; // failed

沒有留言:

張貼留言