Article 56RGP const variable modified by a C pointer

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);

}latest?d=yIl2AUoC8zA latest?i=VlI4icYSdfA:et-bmx6rQsc:F7zBnMy latest?i=VlI4icYSdfA:et-bmx6rQsc:V_sGLiP latest?d=qj6IDK7rITs latest?i=VlI4icYSdfA:et-bmx6rQsc:gIN9vFwVlI4icYSdfA
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments