How come compiler lets this pass?

This is a discussion on How come compiler lets this pass? within the C Programming forums, part of the General Programming Boards category; How come the gnu compiler lets this pass? Code: #include <stdio.h> #include <stdlib.h> int main(void) { char x = 1; ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    How come compiler lets this pass?

    How come the gnu compiler lets this pass?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        char x = 1;
        char const *f = &x;
    
        *f=3;
    
        printf("The value is: %d \n", x);
        return 0;
    }
    $gcc -Wall mod.c -o mod
    mod.c: In function `main':
    mod.c:8: warning: assignment of read-only location
    $./mod
    The value is: 3
    $

    All the compiler does is give a warning, but lets this pass. Is this just how they chose to implement the c compiler?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    What version?
    C:/Dev-Cpp/bin/gcc.exe -c -g -Wall -o "H:/Forum/C/GnuC/main.o" "../main.c"
    ../main.c: In function `main':
    ../main.c:8: error: assignment of read-only location
    c:\Dev-Cpp\bin\make.EXE: *** [H:/Forum/C/GnuC/main.o] Error 1
    Build stopped. c:\Dev-Cpp\bin\make.EXE returned 2
    [edit]
    H:\Forum\C>C:/Dev-Cpp/bin/gcc.exe -v
    Reading specs from C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/specs
    Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as --host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug
    Thread model: win32
    gcc version 3.4.2 (mingw-special)
    Last edited by Dave_Sinkula; 01-14-2006 at 11:14 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    I tested this on gcc version 3.3.3 using Suse Linux 9.1 on an standard PC and
    gcc version 2.95.3 20010315 using FreeBSD 4.6 on some doped out VAX. I got the same error messages on both platforms.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    So in other words, you are using a compiler on a platform that corrects this nonesense.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Perhaps. Would -Werror help?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    I don't know how to put this in quotes: I just tried using -Werror

    $gcc -Werror mod.c -o mod
    mod.c: In function `main':
    mod.c:8: warning: assignment of read-only location
    $./mod
    bash: ./mod: No such file or directory
    $

    The compiler didn't let it pass. That is truly amazing. I didn't realize gcc had this kind of option.

    Thanks. I've been enlightened.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    note: these kind of thing dont happen in C++. the following program is written in C++ and compilers in GNU compiler just with -W flag
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main(void) {
        char x = 1;
        char const *f = &x;
    
        *f=3;
    
        cout<<"The value is: "<<x<<endl;
        return 0;
    }
    /* it gives u an error at first first itself
    const.cpp: In function ‚int main()‚:
    const.cpp:10: error: assignment of read-only location
    */
    ssharish2005

  9. #9
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    FWIW, on gcc 4.0.2, it's an error, whether -W or -Wall is specified or not.

    On a plain vanilla compile, it shows up as an error.

    BTW, the platform is Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lcc win32 compiler download problems
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-01-2004, 08:39 PM
  2. OpenScript2.0 Compiler
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 01:52 PM
  3. Compiler Design... Anyone That Can Help!
    By ComputerNerd888 in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 10:48 AM
  4. Comile problem using latest Dev C++ Compiler
    By shiny_dico_ball in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2003, 06:32 PM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 02:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21