constant datatypes

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

  1. 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??
     
    mallikaarjuna24, Aug 19, 2007
    #1
    1. Advertisements

  2. mallikaarjuna24

    santosh Guest

    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. Advertisements

  3. mallikaarjuna24

    pete Guest

    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.
     
    pete, Aug 19, 2007
    #3
  4. 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, Aug 19, 2007
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.