473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

constant datatypes

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 '07 #1
3 1476
ma*************@gmail.com 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.

Aug 19 '07 #2
santosh wrote:
>
ma*************@gmail.com 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
Aug 19 '07 #3
"ma*************@gmail.com" <ma*************@gmail.comwrites:
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) ks***@mib.org <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"
Aug 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
1
by: Jan Agermose | last post by:
Im writing information into an existing excel document using a connection string like: strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filename + ";Extended Properties=\"Excel...
3
by: Mark | last post by:
What are the best .NET datatypes to handle SQL Server's Float and Real datatypes? I'd like to avoid using the SQL Server specific datatypes like SqlInt32 or similar. Thanks in advance. -Mark
9
by: Mark Rae | last post by:
Hi, I posted a couple of days ago about the possibility of "simulating" in ..NET1.1 the nullable datatypes available in .NET2.0 - I'm nearly there, but require a bit more guidance. Basically,...
4
by: Curious | last post by:
Hi, I have a class with properties and each property has specific datatypes. Is it possible to extract the datatypes of each property found in a class? I would like to extract these...
2
by: Mark Gibson | last post by:
Hello, I've been experimenting with dblink recently, and have encountered some limitations I'd like to discuss. I've been trying to create views of remote tables, like so: CREATE VIEW stuff...
2
by: circuit_breaker | last post by:
Hi, Is there's a built-in object inside of mySQL that hold its various datatypes? As an example, I'd like to populate an array with the following values: "BOOL",...
5
by: michal | last post by:
hi guys, i thought you might be interested in a nice JSON class which converts ASP datatypes (basic datatypes, dictionaries, recordsets, ...) into JSON so that javascript can easily understand it...
1
by: mallikaarjuna24 | last post by:
I am not restricted from doing an assignment operation on a constant datatype ( say, an int ) but just warned of the consequence. why?
6
by: Jeff | last post by:
Dear experts! ..NET 2.0 I'm trying to make an array containg multiple datatypes. This array will consist of 3 items (string, string, integer): my first try was this, (of course it fails)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.