const int * assignment

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

    const int * assignment

    hello,
    const int *ptr1 mean i can change value of ptr1 but not of *ptr1
    right? then why following snippet doesnot give error?
    int val=50;
    const int *ptr1=&val;
    *(int *)ptr1=98; //what is this casting?
    printf("\n%d %d",++val,*ptr1 ); //99 98 is output

    regards,
    rahul

  • Marc Thrun

    #2
    Re: const int * assignment

    rahul8143@gmail .com wrote:[color=blue]
    > hello,
    > const int *ptr1 mean i can change value of ptr1 but not of *ptr1
    > right? then why following snippet doesnot give error?
    > int val=50;
    > const int *ptr1=&val;
    > *(int *)ptr1=98; //what is this casting?
    > printf("\n%d %d",++val,*ptr1 ); //99 98 is output
    >
    > regards,
    > rahul
    >[/color]

    This is due to the type cast which drops the constness of ptr1 (correct
    me if I am wrong, I try getting into C99 myself for now).

    Comment

    • Alexei A. Frounze

      #3
      Re: const int * assignment

      <rahul8143@gmai l.com> wrote in message
      news:1126031693 .256403.293920@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      > hello,
      > const int *ptr1 mean i can change value of ptr1 but not of *ptr1
      > right? then why following snippet doesnot give error?[/color]

      Because as you've already noted, there's a cast in the code below:
      [color=blue]
      > int val=50;
      > const int *ptr1=&val;
      > *(int *)ptr1=98; //what is this casting?[/color]

      You'd have a compilation error if you tried assigning 98 to *ptr1 w/o any
      cast.
      But if I saw something like this in the code, I'd reconsider using it at
      first place as it may be full of hidden bugs... Or if this is your code, If
      I were you, I'd first make sure I understand what such and such C construct
      means and does and only then I'd use it, whenever necessary. Simply put, the
      above doesn't make mich sense.

      Alex


      Comment

      • Peter Nilsson

        #4
        Re: const int * assignment

        rahul8143@gmail .com wrote:[color=blue]
        > hello,
        > const int *ptr1 mean i can change value of ptr1 but not of
        > *ptr1 right?[/color]

        Yes.
        [color=blue]
        > then why following snippet doesnot give error?
        > int val=50;
        > const int *ptr1=&val;
        > *(int *)ptr1=98; //what is this casting?[/color]

        Because here you've overridden the nature of what ptr1 points to.

        When you cast ptr1 you take its pointer _value_ and convert it
        to a different type. As the new pointer type does not point to
        a const qualified object, the compiler is no longer required
        to issue a diagnostic.

        This statement is legal because, in this case, the object being
        pointed to (val) is not itself const qualified. However, this is
        poor programming practice as it is simply an attempt by the
        programmer to undermine the type safety that comes from using
        const in the first place.
        [color=blue]
        > printf("\n%d %d",++val,*ptr1 ); //99 98 is output[/color]

        Here you invoke undefined behaviour, so the code can do anything
        at this point. It's equivalent to...

        printf("\n%d %d", ++val, val);

        ....which is covered in the clc FAQ.

        --
        Peter

        Comment

        • rahul8143@gmail.com

          #5
          Re: const int * assignment


          Peter Nilsson wrote:[color=blue]
          > rahul8143@gmail .com wrote:[color=green]
          > > hello,
          > > const int *ptr1 mean i can change value of ptr1 but not of
          > > *ptr1 right?[/color]
          >
          > Yes.
          >[color=green]
          > > then why following snippet doesnot give error?
          > > int val=50;
          > > const int *ptr1=&val;
          > > *(int *)ptr1=98; //what is this casting?[/color]
          >
          > Because here you've overridden the nature of what ptr1 points to.
          >
          > When you cast ptr1 you take its pointer _value_ and convert it
          > to a different type. As the new pointer type does not point to
          > a const qualified object, the compiler is no longer required
          > to issue a diagnostic.
          >[/color]
          Does you mean *(int *) creates new pointer type which has no name
          but has storage in memory? I am confused about this cast type for const
          types
          *(int *)ptr1=98;
          isn't this be same ptr1? i mean we just type cast and not even store
          its result to any explicit variable?
          [color=blue]
          > This statement is legal because, in this case, the object being
          > pointed to (val) is not itself const qualified. However, this is
          > poor programming practice as it is simply an attempt by the
          > programmer to undermine the type safety that comes from using
          > const in the first place.
          >[color=green]
          > > printf("\n%d %d",++val,*ptr1 ); //99 98 is output[/color]
          >
          > Here you invoke undefined behaviour, so the code can do anything
          > at this point. It's equivalent to...
          >
          > printf("\n%d %d", ++val, val);
          >
          > ...which is covered in the clc FAQ.
          >
          > --
          > Peter[/color]

          Comment

          • Keith Thompson

            #6
            Re: const int * assignment

            rahul8143@gmail .com writes:[color=blue]
            > Peter Nilsson wrote:[color=green]
            >> rahul8143@gmail .com wrote:[color=darkred]
            >> > hello,
            >> > const int *ptr1 mean i can change value of ptr1 but not of
            >> > *ptr1 right?[/color]
            >>
            >> Yes.
            >>[color=darkred]
            >> > then why following snippet doesnot give error?
            >> > int val=50;
            >> > const int *ptr1=&val;
            >> > *(int *)ptr1=98; //what is this casting?[/color]
            >>
            >> Because here you've overridden the nature of what ptr1 points to.
            >>
            >> When you cast ptr1 you take its pointer _value_ and convert it
            >> to a different type. As the new pointer type does not point to
            >> a const qualified object, the compiler is no longer required
            >> to issue a diagnostic.
            >>[/color]
            > Does you mean *(int *) creates new pointer type which has no name
            > but has storage in memory? I am confused about this cast type for const
            > types
            > *(int *)ptr1=98;
            > isn't this be same ptr1? i mean we just type cast and not even store
            > its result to any explicit variable?[/color]

            Terminology quibble: it's called a "cast", not a "type cast".

            A cast is an operator that specifies a type conversion. If it appears
            in an expression, it yields a value as its result; that value needn't
            be stored anywhere in memory. In that sense, it's just like any other
            operator. Given:
            a = b * c + d;
            the "*" operator specifies the result of multiplying b and c, but the
            result of the multiplication needn't be stored. It's just part of the
            expression.

            So let's analyze "*(int *)ptr1=98" from the inside out.

            ptr1 is the name of an object of type pointer-to-const-int. Evaluating
            it in an expression yields a value of type pointer-to-const-int.

            (int*)ptr1 converts that value from pointer-to-const-int to
            pointer-to-int. This can be a dangerous thing to do if the int
            being pointed to really is constant.

            *(int*)ptr1 takes this pointer-to-const-int value and dereferences it,
            yielding an lvalue of type int.

            *(int*)ptr1 = 98 assigns the value 98 to the object pointed to by
            ptr1. We couldn't have done this without the cast because ptr1 points
            to a const int. The cast tells the compiler to pretend that the
            object being pointed to isn't const. As it happens, the object being
            pointed to is "val", which really isn't const, so this is ok.

            Now, if the declaration of val were
            const int val = 50;
            then the compiler would be allowed (but not required) to store val in
            read-only memory. The cast would tell the compiler not to complain
            about the violation of val's constness, but it wouldn't necessarily
            make it possible to change its value. The result would be undefined
            behavior.

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

            • Barry Schwarz

              #7
              Re: const int * assignment

              On Thu, 08 Sep 2005 21:09:57 GMT, Keith Thompson <kst-u@mib.org>
              wrote:
              [color=blue]
              >rahul8143@gmai l.com writes:[color=green]
              >> Peter Nilsson wrote:[color=darkred]
              >>> rahul8143@gmail .com wrote:
              >>> > hello,
              >>> > const int *ptr1 mean i can change value of ptr1 but not of
              >>> > *ptr1 right?
              >>>
              >>> Yes.
              >>>
              >>> > then why following snippet doesnot give error?
              >>> > int val=50;
              >>> > const int *ptr1=&val;
              >>> > *(int *)ptr1=98; //what is this casting?
              >>>
              >>> Because here you've overridden the nature of what ptr1 points to.
              >>>
              >>> When you cast ptr1 you take its pointer _value_ and convert it
              >>> to a different type. As the new pointer type does not point to
              >>> a const qualified object, the compiler is no longer required
              >>> to issue a diagnostic.
              >>>[/color]
              >> Does you mean *(int *) creates new pointer type which has no name
              >> but has storage in memory? I am confused about this cast type for const
              >> types
              >> *(int *)ptr1=98;
              >> isn't this be same ptr1? i mean we just type cast and not even store
              >> its result to any explicit variable?[/color]
              >
              >Terminology quibble: it's called a "cast", not a "type cast".
              >
              >A cast is an operator that specifies a type conversion. If it appears
              >in an expression, it yields a value as its result; that value needn't
              >be stored anywhere in memory. In that sense, it's just like any other
              >operator. Given:
              > a = b * c + d;
              >the "*" operator specifies the result of multiplying b and c, but the
              >result of the multiplication needn't be stored. It's just part of the
              >expression.
              >
              >So let's analyze "*(int *)ptr1=98" from the inside out.
              >
              >ptr1 is the name of an object of type pointer-to-const-int. Evaluating
              >it in an expression yields a value of type pointer-to-const-int.
              >
              >(int*)ptr1 converts that value from pointer-to-const-int to
              >pointer-to-int. This can be a dangerous thing to do if the int
              >being pointed to really is constant.
              >
              >*(int*)ptr1 takes this pointer-to-const-int value and dereferences it,
              >yielding an lvalue of type int.[/color]

              Now it just a pointer to int, no const.
              [color=blue]
              >
              >*(int*)ptr1 = 98 assigns the value 98 to the object pointed to by
              >ptr1. We couldn't have done this without the cast because ptr1 points
              >to a const int. The cast tells the compiler to pretend that the
              >object being pointed to isn't const. As it happens, the object being
              >pointed to is "val", which really isn't const, so this is ok.
              >
              >Now, if the declaration of val were
              > const int val = 50;
              >then the compiler would be allowed (but not required) to store val in
              >read-only memory. The cast would tell the compiler not to complain
              >about the violation of val's constness, but it wouldn't necessarily
              >make it possible to change its value. The result would be undefined
              >behavior.[/color]


              <<Remove the del for email>>

              Comment

              • Keith Thompson

                #8
                Re: const int * assignment

                Barry Schwarz <schwarzb@deloz .net> writes:[color=blue]
                > On Thu, 08 Sep 2005 21:09:57 GMT, Keith Thompson <kst-u@mib.org>
                > wrote:[/color]
                [...][color=blue][color=green]
                >>*(int*)ptr1 takes this pointer-to-const-int value and dereferences it,
                >>yielding an lvalue of type int.[/color]
                >
                > Now it just a pointer to int, no const.[/color]

                Quite right, thanks for the correction.

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

                Working...