const variable reassignment

Discussion in 'C Programming' started by rahul8143@gmail.com, Aug 21, 2005.

  1. Guest

    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?
     
    , Aug 21, 2005
    #1
    1. Advertising

  2. akarl Guest

    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
     
    akarl, Aug 21, 2005
    #2
    1. Advertising

  3. Flash Gordon Guest

    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.
     
    Flash Gordon, Aug 21, 2005
    #3
  4. Cong Wang Guest

    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.
     
    Cong Wang, Aug 21, 2005
    #4
  5. 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?"
     
    Emmanuel Delahaye, Aug 21, 2005
    #5
  6. Flash Gordon Guest

    Cong Wang wrote:
    > 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.
     
    Flash Gordon, Aug 21, 2005
    #6
  7. Flash Gordon <> writes:
    > 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) <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.
     
    Keith Thompson, Aug 21, 2005
    #7
  8. Flash Gordon Guest

    Keith Thompson wrote:
    > Flash Gordon <> writes:
    >
    >> 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.
     
    Flash Gordon, Aug 21, 2005
    #8
  9. Flash Gordon <> writes:
    > Keith Thompson wrote:
    >> Flash Gordon <> 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) <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.
     
    Keith Thompson, Aug 21, 2005
    #9
  10. Flash Gordon Guest

    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.
     
    Flash Gordon, Aug 21, 2005
    #10
  11. elsdy Guest

    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.
     
    elsdy, Aug 24, 2005
    #11
  12. Flash Gordon Guest

    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.
     
    Flash Gordon, Aug 24, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oplec
    Replies:
    6
    Views:
    531
    Oplec
    Nov 1, 2003
  2. Jp Calderone
    Replies:
    1
    Views:
    518
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Jul 6, 2003
  3. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  4. ssecorp
    Replies:
    5
    Views:
    322
    Terry Reedy
    Jul 12, 2008
  5. Brendan Miller
    Replies:
    0
    Views:
    216
    Brendan Miller
    Jan 31, 2009
Loading...

Share This Page