Using the -Wcast-qual parameter to verify compilation

I read an introduction to gcc that says:

'- Wcast-qual this parameter warns about pointers that are deleted to remove a classifier such as const. For example, the following function discards the const specifier from its input argument, allowing it to be overwritten:

 void
       f (const char * str)
       {
         char * s = (char *)str;
         s[0] = ’\0’; }

Modifying the original contents of str is a violation of its const. This parameter will warn about the variable str, which allows you to change the string.

I tried to repeat this, expecting a warning, but there are no warnings when compiling it . My code is as follows:

#include <stdio.h>

void f(const char * str);

int main() {
    char Str = 'a';
    char * myStr;
    myStr = & Str;
    printf ("result: %c \n", * myStr);
    f(myStr);
    printf ("result: %c \n", * myStr);
    return 0;
}


void
f (const char * str)
{
  char * s = (char *)str;
  s[0] = '\0';
}

team: gcc -Wcast-qual castqual.c

Can anyone explain this inconsistency?

+4
1

, OP mac, gcc clang - llvm.

clang, , , . XCode 7/clang 7 ; gcc, clang.

gcc, , , homebrew macports. gcc , , gcc-4.8, gcc-5 .. ( ). gcc, : gcc-4.8 -Wcast-qual testcode.c. autoconf, export CC=gcc-4.8; export CXX=g++-4.8 .., .

, gcc, clang, gcc --version. clang , :

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.4.0
Thread model: posix

gcc :

$ gcc-5 --version
gcc-5 (Homebrew gcc 5.1.0) 5.1.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

imho : gcc-4.8, gcc, , .

+1

All Articles