constant datatypes

Discussion in 'C Programming' started by mallikaarjuna24@gmail.com, Aug 19, 2007.

  1. Guest

    why my const integer is strangely accepting an assignment statement or
    an increment operation on it just with a warning ??!!
    It should restrict me from doing that.. isn't it??
    , Aug 19, 2007
    #1
    1. Advertising

  2. santosh Guest

    wrote:

    > why my const integer is strangely accepting an assignment statement or
    > an increment operation on it just with a warning ??!!
    > It should restrict me from doing that.. isn't it??


    Not in C. In C, the result of attempting to modify const qualified objects
    is implementation dependent. IOW, don't do it.
    santosh, Aug 19, 2007
    #2
    1. Advertising

  3. pete Guest

    santosh wrote:
    >
    > wrote:
    >
    > > why my const integer is strangely
    > > accepting an assignment statement or
    > > an increment operation on it just with a warning ??!!
    > > It should restrict me from doing that.. isn't it??

    >
    > Not in C. In C, the result of attempting to modify
    > const qualified objects is implementation dependent.


    No, it's undefined, rather than implementation dependent.
    A correct C program may exhibit implementation defined behavior.
    A correct C program may *not* contain undefined behavior.

    N869
    6.7.3 Type qualifiers

    [#5] If an attempt is made to modify an object defined with
    a const-qualified type through use of an lvalue with non-
    const-qualified type, the behavior is undefined.

    > IOW, don't do it.


    --
    pete
    pete, Aug 19, 2007
    #3
  4. "" <> writes:
    > why my const integer is strangely accepting an assignment statement or
    > an increment operation on it just with a warning ??!!
    > It should restrict me from doing that.. isn't it??


    Next time, please post a small program that exhibits the problem,
    along with the warning message your compiler produces.
    (Copy-and-paste both the program and the message; don't attempt to
    re-type them.)

    There are various ways you can bypass const qualification and
    *attempt* to assign a value to a const-qualified object (for example,
    by using a pointer cast). Any attempt to do so invokes undefined
    behavior; the compiler is not required (and typically is not able) to
    diagnose the problem, but any arbitrarily bad thing can happen when
    you execute the code. In the best case, your program immediately
    crashes. In the worst case, it silently "works". In the *really*
    worst case, it works at first, but fails catastrophically at the most
    inconvenient possible moment. So don't do that.

    But since you mentioned a warning, that's probably not what happened
    in your case. Somebody posted a possible example:

    int main(void) {
    const int i;
    i = 0;
    }

    The assignment is invalid. This is an example of a class of errors
    that the standard calls "constraint violations". The standard
    requires a compiler to issue a "diagnostic message", but it says very
    little about what a "diagnostic message" is. In particular, the
    compiler is not required to reject your program; once it's issued a
    diagnostic message, it's done its job. (It's also allowed to issue
    diagnostic messages for things that aren't constraint violations.)

    The lesson: Don't ignore warnings.

    And if you had posted your actual code, we wouldn't have had to guess,
    and our responses could probably have been shorter, less numerous, and
    more to the point.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,086
  2. Martin Magnusson
    Replies:
    2
    Views:
    503
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    558
    Tim Roberts
    Nov 23, 2006
  4. constant datatypes

    , Aug 19, 2007, in forum: C Programming
    Replies:
    2
    Views:
    292
    santosh
    Aug 19, 2007
  5. Veli-Pekka Tätilä
    Replies:
    2
    Views:
    117
    Eric J. Roode
    Aug 27, 2005
Loading...

Share This Page