const variable modified by a C pointer
by atharvdesai1996 from LinuxQuestions.org on (#56RGP)
On running this simple C code, it shows a warning 'initialization discards const' qualifier from pointer target type' and modifies the const var which I thought shouln't be modified. Could someone please explain?
#include <stdio.h>
#include<stdlib.h>
int main()
{
const int var=10;
int * ptr = &var;
printf(" %d \n", *ptr);
*ptr = 20;
printf(" %d \n", *ptr);
}


#include <stdio.h>
#include<stdlib.h>
int main()
{
const int var=10;
int * ptr = &var;
printf(" %d \n", *ptr);
*ptr = 20;
printf(" %d \n", *ptr);
}