Pointers
Pointer To Const
A pointer to const allows reading through the pointer while protecting the pointed-to value from writes through that pointer.
Pointer To Const
pointer_to_const.c
#include <stdio.h>
int main(void) {
int first = ;
int second = 6;
int useSecond = ;
const int *ptr = &first;
if (useSecond) {
ptr = &second;
}
printf("read=%d\n", *ptr);
return 0;
}
#include <stdio.h>
int main(void) {
int first = ;
int second = 6;
int useSecond = ;
const int *ptr = &first;
if (useSecond) {
ptr = &second;
}
printf("read=%d\n", *ptr);
return 0;
}
#include <stdio.h>
int main(void) {
int first = ;
int second = 6;
int useSecond = ;
const int *ptr = &first;
if (useSecond) {
ptr = &second;
}
printf("read=%d\n", *ptr);
return 0;
}
#include <stdio.h>
int main(void) {
int first = ;
int second = 6;
int useSecond = ;
const int *ptr = &first;
if (useSecond) {
ptr = &second;
}
printf("read=%d\n", *ptr);
return 0;
}
#include <stdio.h>
int main(void) {
int first = ;
int second = 6;
int useSecond = ;
const int *ptr = &first;
if (useSecond) {
ptr = &second;
}
printf("read=%d\n", *ptr);
return 0;
}
#include <stdio.h>
int main(void) {
int first = ;
int second = 6;
int useSecond = ;
const int *ptr = &first;
if (useSecond) {
ptr = &second;
}
printf("read=%d\n", *ptr);
return 0;
}
const pointee
`const int *ptr` means the `int` cannot be changed through `ptr`.
reassign pointer
The pointer itself can still be changed to read a different object.