It changes who Const modifies.
int myint = 3;
int *const myptr = &myint;
If declared together, You can change the value of myint with myptr, It is impossible for myptr to point to a value other than myint.
For example,
int main(void) {
int myint1 = 3, myint2 = 5;
int *const myptr = &myint1;
printf("value of myptr = address value of myint1: %p\n", myptr);
/*Possible*/
*myptr = 4; //myint value: 3->4
/*Impossible*/
myptr = &myint2;
}
Output: value of myptr = address value of myint1: 0x7fff5fbff76c
where the value 0x7fff5fbff76c of myptr is const. However, it is possible to change the value in address 0x7fff5fbff76c.
int myint = 3;
const int * myptr = &myint;
If declared together, You can't change the value of myint with myptr, Myptr may point to a value other than myint.
For example,
int main(void) {
int myint1 = 3, myint2 = 5;
const int *myptr = &myint1;
printf("value of myptr = address value of myint1: %p\n", myptr);
/*Possible*/
myptr = &myint2;
printf("value of myptr = address value of myint2: %p\n", myptr);
/*Impossible*/
//*myptr = 4;
}
Output:
value of myptr = address value of myint1: 0x7fff5fbff76c
The value of myptr = the address value of myint2: 0x7fff5fbff768
© 2024 OneMinuteCode. All rights reserved.