473,804 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1492
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.comwrit es:
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 catastrophicall y 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_Keit h) 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
6157
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
1
10630
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 8.0;HDR=yes;\""; and one of to methods for inserting data. First I simply tried building insert statements as strings "insert into (, ) values ('Some text', 42)"
3
11853
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
3257
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, I have a class file which contains the various class definitions, as follows: using System;
4
1467
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 datatypes so that I can build a dataset schema structure at runtime.
2
2335
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 AS SELECT * FROM dblink(' ... remote connection info ... ', 'SELECT id, title, title_idx FROM stuff')
2
1940
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", "INT","VARCHAR","MEDUIMINT","ENUM" and so on. Thanks.
5
3823
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 ... you'll find the demonstration and the download here http://fabiankoehler.de/wdb/2007/04/26/generate-json-from-asp-datatypes/
1
317
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
5567
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) string param = new string; param = "2007-08-01";
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.