assigning to const int via pointer

Joe Steeve joe_steeve@gmx.net
Thu Dec 30 06:27:00 GMT 2004


The following code assigns a value to a `const int` via a
pointer.,

#include <stdio.h>

int 
main()
{
  const int x=5;
  int *ptr;
  ptr = &x;
  *ptr = 10;
  printf("%d",x);
}

The code gives `10` for the following compilation

    $gcc -o test test.c

It gives `5` when using optimisations switches.,

    $gcc -o test -O2 test.c

Feature or bug or any explanation for this? 

-- 
.O. A proud GNU user
..O http://www.joesteeve.tk/
OOO http://gnukid.5gigs.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 188 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20041230/644c5f3a/attachment.sig>


More information about the Gcc-help mailing list