How far does const go?

This is a discussion on How far does const go? within the C Programming forums, part of the General Programming Boards category; Hi, everyone. I have written the following program, which compiles and runs on gcc/Ubuntu. Is it legal C? Code: int ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    How far does const go?

    Hi, everyone.

    I have written the following program, which compiles and runs on gcc/Ubuntu. Is it legal C?

    Code:
    int main()
    {
     const int a = 1;
     int* b = &a;
     *b = 2;
     return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Do you get this warning?

    warning: initialization discards qualifiers from pointer target type

    If you try to modify an object declared const, you're into undefined behaviour territory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

Tags for this Thread


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