473,385 Members | 1,630 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,385 software developers and data experts.

const variable reassignment

hello,
Check following code that changes const i value.
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
(*p)++;
printf("\n %d",i);
return 0;
}

we know that (*p)++ is same as *p=*p+1 then why i am not allowed to
directly assign new value to i
i.e. *p=15;
Isn't *p=*p+1 is reassinging new value to i?

Nov 15 '05 #1
11 1304
ra*******@gmail.com wrote:
hello,
Check following code that changes const i value.
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
(*p)++;
printf("\n %d",i);
return 0;
}

we know that (*p)++ is same as *p=*p+1 then why i am not allowed to
directly assign new value to i
i.e. *p=15;
Isn't *p=*p+1 is reassinging new value to i?


In your code you say that `i' should be constant so what do you expect?

August

Nov 15 '05 #2

ra*******@gmail.com wrote:
hello,
Check following code that changes const i value.
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
(*p)++;
printf("\n %d",i);
return 0;
}

we know that (*p)++ is same as *p=*p+1 then why i am not allowed to
directly assign new value to i
i.e. *p=15;
Isn't *p=*p+1 is reassinging new value to i?


Modifying a const through a pointer is an undefined behaviour.
Exactly,(*p)++ is different from *p=*p+1.

Nov 15 '05 #3
ra*******@gmail.com wrote:
hello,
Check following code that changes const i value.
Which means it is invoking undefined behaviour.
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
Doesn't you compiler generate a warning for this? If not, you need to
turn up the warnings it generates to a sensible level.
(*p)++;
printf("\n %d",i);
return 0;
}

we know that (*p)++ is same as *p=*p+1 then why i am not allowed to
directly assign new value to i
i.e. *p=15;
You are not allowed to because the standard says it invoked undefined
behaviour, which means that anything can happen. Using the pointer to
increment it is also not allowed, and I don't know what makes you think
it is allowed.
Isn't *p=*p+1 is reassinging new value to i?


Yes, and it is not allowed.

Only declare things as const if they will never be modified, that is
what const is for. If they will be modified, then obviously they should
not be declared const.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #4
ra*******@gmail.com wrote on 21/08/05 :
Check following code that changes const i value.
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
I have a warning here with my compiler. (wrong type).
(*p)++;
printf("\n %d",i);
return 0;
}

we know that (*p)++ is same as *p=*p+1 then why i am not allowed to
directly assign new value to i
Because the 'const' qualifier explicitely prevents against it.
i.e. *p=15;
Isn't *p=*p+1 is reassinging new value to i?


Yes. It may work or not.

The whole thing is a design issue. If a variable has been qualified
'const', it means, in the developper's mind, that its value is
invariant, so, trying to modify it is a violation of a design rule.

Of course, you can override it via a pointer and some ugly typecast,
but under your own responsability. Don't come here and cry if the whole
program is not yet working as expected...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
Nov 15 '05 #5
Cong Wang wrote:
ra*******@gmail.com wrote:
hello,
Check following code that changes const i value.
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
(*p)++;
printf("\n %d",i);
return 0;
}

we know that (*p)++ is same as *p=*p+1 then why i am not allowed to
directly assign new value to i
i.e. *p=15;
Isn't *p=*p+1 is reassinging new value to i?
Modifying a const through a pointer is an undefined behaviour.


Yes.
Exactly,(*p)++ is different from *p=*p+1.


What do you mean? (*p)++ increments the value stored at *p, i.e. in
terms of the effect on i in the abive code it is the *same* as *p=*p+1
and is undefined behaviour.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #6
Flash Gordon <sp**@flash-gordon.me.uk> writes:
ra*******@gmail.com wrote: [...]
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
(*p)++;
printf("\n %d",i);
return 0;
}

[...] You are not allowed to because the standard says it invoked undefined
behaviour, which means that anything can happen. Using the pointer to
increment it is also not allowed, and I don't know what makes you think
it is allowed.


You're not allowed to modify i directly, as in
i ++;
because it's a constraint violation.

You *are* "allowed" to modify it indirectly, in the sense that the
compiler isn't (necessarily) going to stop you. Declaring i as
"const int" means you're promising not to modify i; it deons't mean
that the compiler will always hold you to your promise. If you lie to
the compiler by declaring i as "const" and then modifying it, the
compiler isn't obligated to do anything in particular.

--
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.
Nov 15 '05 #7
Keith Thompson wrote:
Flash Gordon <sp**@flash-gordon.me.uk> writes:
ra*******@gmail.com wrote:


[...]
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
(*p)++;
printf("\n %d",i);
return 0;
}


[...]
You are not allowed to because the standard says it invoked undefined
behaviour, which means that anything can happen. Using the pointer to
increment it is also not allowed, and I don't know what makes you think
it is allowed.

You're not allowed to modify i directly, as in
i ++;
because it's a constraint violation.

You *are* "allowed" to modify it indirectly, in the sense that the
compiler isn't (necessarily) going to stop you. Declaring i as
"const int" means you're promising not to modify i; it deons't mean
that the compiler will always hold you to your promise. If you lie to
the compiler by declaring i as "const" and then modifying it, the
compiler isn't obligated to do anything in particular.


Including not being obliged to produce a program that does what the OP
expects. I know the compiler does not have to produce a diagnostic, but
since the standard does not define the term, "not allowed to," as far as
I am aware I don't think it unreasonable to say that you are not allowed
to do it.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #8
Flash Gordon <sp**@flash-gordon.me.uk> writes:
Keith Thompson wrote:
Flash Gordon <sp**@flash-gordon.me.uk> writes: [...]
You are not allowed to because the standard says it invoked
undefined behaviour, which means that anything can happen. Using
the pointer to increment it is also not allowed, and I don't know
what makes you think it is allowed.

You're not allowed to modify i directly, as in
i ++;
because it's a constraint violation.
You *are* "allowed" to modify it indirectly, in the sense that the
compiler isn't (necessarily) going to stop you. Declaring i as
"const int" means you're promising not to modify i; it deons't mean
that the compiler will always hold you to your promise. If you lie to
the compiler by declaring i as "const" and then modifying it, the
compiler isn't obligated to do anything in particular.


Including not being obliged to produce a program that does what the OP
expects. I know the compiler does not have to produce a diagnostic,
but since the standard does not define the term, "not allowed to," as
far as I am aware I don't think it unreasonable to say that you are
not allowed to do it.


Sure, but someone unfamiliar with the nuances of constraint violations
and undefined behavior might easily read "not allowed to" as implying
that the error will be diagnosed.

--
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.
Nov 15 '05 #9
Keith Thompson wrote:

<snip>
Sure, but someone unfamiliar with the nuances of constraint violations
and undefined behavior might easily read "not allowed to" as implying
that the error will be diagnosed.


OK, I'll accept that people might make that inference. I don't having
not always been told off for doing things I was not allowed to do, but I
can see that others might.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #10
I try to compile and run the same code you show.
but i have got massage that say cannot convert from 'const int *__w64'
to 'int *'
I used to try that with visual 2005 express edition beta.

I think that if the compiler is correct, error message is printed with
above message. and
wrong part is that p=&i.

Nov 15 '05 #11
elsdy wrote:
I try to compile and run the same code you show.
What code? How do you know that everyone has seen the post you are
replying to? Even if they have, a number of us set news readers to only
show unread posts so that we can easily see what is new.

Instructions have been posted here LITERALLY hundreds of times saying
how you can post properly using that horrible google interface. Read
CBFalconer's sig for a start. Also read the group (like you should
ALWAYS do before posting to the first time) an you will also see lots of
complaints.
but i have got massage that say cannot convert from 'const int *__w64'
to 'int *'
__w64 is not part of standard C so we don't know anything about it here.
I used to try that with visual 2005 express edition beta.

I think that if the compiler is correct, error message is printed with
above message. and
wrong part is that p=&i.


Well, without knowing the types of p and i since you don't provide any
context how can we know WTF is going on?
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #12

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

Similar topics

6
by: Oplec | last post by:
Hi, I thought that I understood how C++ allows for the declaration and defining of variables within an if() statement and how the declared variable can be used until the end of the major if()...
31
by: Ben | last post by:
For many times, I've found myself changing my member variables from const back to non-const. No matter how good the original objective was, there was always at least one reason not to use const...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
2
by: Gigsman | last post by:
public void Foo(string one) { one = "two"; } main() { string one = "one"; Foo(one);
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: subramanian100in | last post by:
I read in C++ Primer 4th Edition by Stanley Lippman, in page 57, that const variables at global scope are local to a file by default. What is the advantage of this rule ? Suppose I have the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.