const variable reassignment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rahul8143@gmail.com

    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?

  • akarl

    #2
    Re: const variable reassignment

    rahul8143@gmail .com wrote:[color=blue]
    > 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?[/color]

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

    August

    Comment

    • Cong Wang

      #3
      Re: const variable reassignment


      rahul8143@gmail .com wrote:[color=blue]
      > 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?[/color]

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

      Comment

      • Flash Gordon

        #4
        Re: const variable reassignment

        rahul8143@gmail .com wrote:[color=blue]
        > hello,
        > Check following code that changes const i value.[/color]

        Which means it is invoking undefined behaviour.
        [color=blue]
        > include <stdio.h>
        > int main()
        > {
        > const int i=10;
        > int *p;
        > p=&i;[/color]

        Doesn't you compiler generate a warning for this? If not, you need to
        turn up the warnings it generates to a sensible level.
        [color=blue]
        > (*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;[/color]

        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.
        [color=blue]
        > Isn't *p=*p+1 is reassinging new value to i?[/color]

        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.

        Comment

        • Emmanuel Delahaye

          #5
          Re: const variable reassignment

          rahul8143@gmail .com wrote on 21/08/05 :[color=blue]
          > Check following code that changes const i value.
          > include <stdio.h>
          > int main()
          > {
          > const int i=10;
          > int *p;
          > p=&i;[/color]

          I have a warning here with my compiler. (wrong type).
          [color=blue]
          > (*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[/color]

          Because the 'const' qualifier explicitely prevents against it.
          [color=blue]
          > i.e. *p=15;
          > Isn't *p=*p+1 is reassinging new value to i?[/color]

          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?"


          Comment

          • Flash Gordon

            #6
            Re: const variable reassignment

            Cong Wang wrote:[color=blue]
            > rahul8143@gmail .com wrote:
            >[color=green]
            >>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?[/color]
            >
            > Modifying a const through a pointer is an undefined behaviour.[/color]

            Yes.
            [color=blue]
            > Exactly,(*p)++ is different from *p=*p+1.[/color]

            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.

            Comment

            • Keith Thompson

              #7
              Re: const variable reassignment

              Flash Gordon <spam@flash-gordon.me.uk> writes:[color=blue]
              > rahul8143@gmail .com wrote:[/color]
              [...][color=blue][color=green]
              >> include <stdio.h>
              >> int main()
              >> {
              >> const int i=10;
              >> int *p;
              >> p=&i;
              >> (*p)++;
              >> printf("\n %d",i);
              >> return 0;
              >> }[/color][/color]
              [...][color=blue]
              > 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.[/color]

              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_Keit h) kst-u@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.

              Comment

              • Flash Gordon

                #8
                Re: const variable reassignment

                Keith Thompson wrote:[color=blue]
                > Flash Gordon <spam@flash-gordon.me.uk> writes:
                >[color=green]
                >>rahul8143@gma il.com wrote:[/color]
                >
                > [...]
                >[color=green][color=darkred]
                >>>include <stdio.h>
                >>>int main()
                >>>{
                >>>const int i=10;
                >>>int *p;
                >>>p=&i;
                >>>(*p)++;
                >>>printf("\n %d",i);
                >>>return 0;
                >>>}[/color][/color]
                >
                > [...]
                >[color=green]
                >>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.[/color]
                >
                >
                > 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.[/color]

                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.

                Comment

                • Keith Thompson

                  #9
                  Re: const variable reassignment

                  Flash Gordon <spam@flash-gordon.me.uk> writes:[color=blue]
                  > Keith Thompson wrote:[color=green]
                  >> Flash Gordon <spam@flash-gordon.me.uk> writes:[/color][/color]
                  [...][color=blue][color=green][color=darkred]
                  >>> 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.[/color]
                  >> 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.[/color]
                  >
                  > 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.[/color]

                  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_Keit h) kst-u@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.

                  Comment

                  • Flash Gordon

                    #10
                    Re: const variable reassignment

                    Keith Thompson wrote:

                    <snip>
                    [color=blue]
                    > 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.[/color]

                    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.

                    Comment

                    • elsdy

                      #11
                      Re: const variable reassignment

                      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.

                      Comment

                      • Flash Gordon

                        #12
                        Re: const variable reassignment

                        elsdy wrote:[color=blue]
                        > I try to compile and run the same code you show.[/color]

                        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.
                        [color=blue]
                        > but i have got massage that say cannot convert from 'const int *__w64'
                        > to 'int *'[/color]

                        __w64 is not part of standard C so we don't know anything about it here.
                        [color=blue]
                        > 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.[/color]

                        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.

                        Comment

                        Working...