Rohit kumar Chandel said:
Hi all,
I have a doubt in const keyword. My understanding says that a variable
declared const can not be modified by the module it is defined in. Then
consider the following code segment:
main()
{
const int p =5;
int* pp = &p;
++(*pp);
printf("%d\n",p);
}
To my surprise it increments value of p by one where p has to be a
constant. Please clarify my doubt.
you may be using a compiler which is fully ANSI compliant. I get this
warning when I try to compile the same code:
C:\>gcc const1.c
const1.c: In function `main':
const1.c:4: warning: initialization discards qualifiers from pointer target
type.
This warning may as well be an error be setting some other options. Or in in
other compilers its may be error by default.
Is const really const ? Just run the program with the change of making p
into a global variable and you get this result:
C:\>a.exe
13 [main] a 3232 handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
10820 [main] a 3232 open_stackdumpfile: Dumping stack trace to
a.exe.stackdump
Now you can decide for yourself !
Regards,
Ravishankar