Ansi violation ?

llewelly@edevnull.com llewelly@edevnull.com
Tue Sep 5 10:23:00 GMT 2000


Marc Olzheim <marcolz@stack.nl> writes:

> Version:
> gcc 2.95.2 on FreeBSD 4.1
> 
> For 2.7.2.3 it works ok.
> 
> commandline:
> gcc -c -o const.o -Wcast-qual -ansi const.c

>From http://gcc.gnu.org/onlinedocs/gcc_2.html#SEC9

-Wcast-qual
          Warn whenever a pointer is cast so as to remove a type
          qualifier from the target type. For example, warn if a const
          char * is cast to an ordinary char *.

If you remove the flag -Wcast-qual, the warning disappears.


> Produces:
> const.c:16: warning: cast discards qualifiers from pointer target type
> 
> Quoting http://www.eskimo.com/~scs/C-faq/q11.10.html:
> 
> ---
> You can use a pointer-to-T (for any type T) where a pointer-to-const-T is
> expected. However, the rule (an explicit exception) which permits slight
> mismatches in qualified pointer types is not applied recursively, but only at
> the top level.
> ---
> 
> I know I shouldn't be doing this anyway, but sometimes include files
> do not yet know of 'const' and you have to do it.
> 
> const.c attached.
> 
> Marc
> 
> char	*
> func(void)
> {
> 	char		*foo;
> 	const char	*cfoo = "bar";
> 
> 	/*  From http://www.eskimo.com/~scs/C-faq/q11.10.html:
> 	 *
> 	 *  References: ANSI Sec. 3.1.2.6, Sec. 3.3.16.1, Sec. 3.5.3 
> 	 *  ISO Sec. 6.1.2.6, Sec. 6.3.16.1, Sec. 6.5.3 
> 	 *  H&S Sec. 7.9.1 pp. 221-2
> 	 *
> 	 *  Use -Wcast-qual with gcc.
> 	 *  gcc 2.7.2.3 ok, gcc 2.95.2 not ok.
> 	 */
> 	foo = *((char *const *) &cfoo);
> 
> 	return (foo);
> }



More information about the Gcc-bugs mailing list