Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 02 Dec 2004 16:12:53 +0900
From:      Rob <spamrefuse@yahoo.com>
To:        FreeBSD <freebsd-questions@freebsd.org>
Subject:   gcc violates const-ness of variable?
Message-ID:  <41AEC075.50207@yahoo.com>

next in thread | raw e-mail | index | archive | help
Hi,

This should probably be discussed in the GNU gcc mailinglist.
But I'm more familiar here, and I first want to share it here
with other FreeBSD users.

I'm surprised about this piece of code:

  #include<stdio.h>
  int main()
  {
   const int n = 0;
   scanf("%d", &n);
   printf("%d\n", n);
   return 0;
  }

With gcc compiler, the constant variable 'n' will be overwritten
by the scanf statement. With g++ it (silently) is not.
So, I get following:

$ gcc -W -Wall code.c
code.c: In function `main':
code.c:5: warning: writing into constant object (arg 2)
$ ./a.out
9
9
$ g++ -W -Wall code.c
code.c: In function `int main()':
code.c:5: warning: writing into constant object (arg 2)
$ ./a.out
9
0

Is this a bug in gcc, or a feature?

Rob.




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?41AEC075.50207>