constant datatypes

M

mallikaarjuna24

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??
 
S

santosh

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

pete

santosh said:
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.
 
K

Keith Thompson

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top