17 May 2012

Pointer and Const

int n = 5; //normal int variable
int *const nptr = &n; //nptr always point to n, cannot be changed
*nptr = 6; //OK since n is non-const

const int *cnptr = &n; //treats the pointed variable as a const
n = 7; //OK
*cnptr = 8; //NG since n is treated as const

const int cn;
const int *const cnptr = &n; //cannot redirect pointer, cannot change value


source: pointers and const

No comments:

Post a Comment