I recently took a look at how pointers work with constants in C, and I read that pointers on constants could lead to modify them:
#include <stdio.h>
int main (int argc, char ** argv) {
const int x = 1;
int *y;
y = &x;
*y += 1;
printf("x = %d\n", x); // Prints: 2
printf("y = %d\n", *y); // Prints: 2
return 0;
}
Here, I define a constant called x
and make a pointer from it so I can modify its value. This means x
is not really constant. Is there a way to make it really constant?
EDIT : This question is not a duplicate of this one as I am asking here how we can make a real constant, not how to change a constant through a pointer.