Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > const variable reassignment

Reply
Thread Tools

const variable reassignment

 
 
rahul8143@gmail.com
Guest
Posts: n/a
 
      08-21-2005
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?

 
Reply With Quote
 
 
 
 
akarl
Guest
Posts: n/a
 
      08-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      08-21-2005
(E-Mail Removed) 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.
 
Reply With Quote
 
Cong Wang
Guest
Posts: n/a
 
      08-21-2005

(E-Mail Removed) 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.

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      08-21-2005
(E-Mail Removed) 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?"


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-21-2005
Cong Wang wrote:
> (E-Mail Removed) 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-21-2005
Flash Gordon <(E-Mail Removed)> writes:
> (E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-21-2005
Keith Thompson wrote:
> Flash Gordon <(E-Mail Removed)> writes:
>
>>(E-Mail Removed) 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-21-2005
Flash Gordon <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> Flash Gordon <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-21-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
import reassignment different at module and function scope Brendan Miller Python 0 01-31-2009 07:31 AM
Weird lambda rebinding/reassignment without me doing it ssecorp Python 5 07-12-2008 08:32 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Reassignment to a const variable that was declared in an if() Oplec C++ 6 11-01-2003 03:06 AM
tp_base, tp_basesize, and __slots__ instance __class__ reassignment Jp Calderone Python 1 07-06-2003 07:17 AM



Advertisments