explanation needed on const pointers

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

    explanation needed on const pointers

    I need complete explanation on constant pointers.

    e.g

    char *const ptr ;
    const char *ptr ;
    char *const* ptr;

    please explain me the differences.

  • Sreekanth

    #2
    Re: explanation needed on const pointers

    Hi,
    You can find out something regarding the const pointer Useage at
    following URL:

    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!



    deepunayak@gmai l.com wrote:[color=blue]
    > I need complete explanation on constant pointers.
    >
    > e.g
    >
    > char *const ptr ;
    > const char *ptr ;
    > char *const* ptr;
    >
    > please explain me the differences.[/color]

    Comment

    • tatto0_2000@yahoo.com

      #3
      Re: explanation needed on const pointers


      deepunayak@gmai l.com wrote:[color=blue]
      > I need complete explanation on constant pointers.
      >
      > e.g
      >
      > char *const ptr ;
      > const char *ptr ;
      > char *const* ptr;
      >
      > please explain me the differences.[/color]

      Is this ==== char *const* ptr; ====== declaration valid ??

      Comment

      • Pedro Graca

        #4
        Re: explanation needed on const pointers

        deepunayak@gmai l.com wrote:[color=blue]
        > I need complete explanation on constant pointers.
        >
        > e.g
        >
        > char *const ptr ;[/color]

        ptr = NULL; /* not allowed */
        ptr[0] = 42; /* allowed */
        [color=blue]
        > const char *ptr ;[/color]

        ptr = NULL; /* allowed */
        ptr[0] = 42; /* not allowed */
        [color=blue]
        > char *const* ptr;[/color]

        ptr = NULL; /* allowed */
        ptr[0] = NULL; /* not allowed */
        ptr[0][0] = 42; /* allowed */

        --
        If you're posting through Google read <http://cfaj.freeshell. org/google>

        Comment

        • Ben Bacarisse

          #5
          Re: explanation needed on const pointers

          On Wed, 15 Feb 2006 23:20:17 -0800, deepunayak@gmai l.com wrote:
          [color=blue]
          > I need complete explanation on constant pointers.[/color]

          Ah, you'll need a good book for that.
          [color=blue]
          > char *const ptr ;
          > const char *ptr ;
          > char *const* ptr;
          >
          > please explain me the differences.[/color]

          If you use the rule I outlined in:



          you can read these as English words which will help to get you started.

          <OT>What is the best way to reference a previous posting? The above
          looks horrible and I can't from the style is the URI in long-lived or
          not.</OT>

          --
          Ben.

          Comment

          • Seemanta Dutta

            #6
            Re: explanation needed on const pointers

            tatto0_2000@yah oo.com wrote:[color=blue]
            > deepunayak@gmai l.com wrote:[color=green]
            >> I need complete explanation on constant pointers.
            >>
            >> e.g
            >>
            >> char *const ptr ;[/color][/color]
            value pointed by ptr is constant.
            ptr can be changed.
            [color=blue][color=green]
            >> const char *ptr ;[/color][/color]
            value pointed by ptr can change.
            ptr itself can't change.[color=blue][color=green]
            >> char *const* ptr;
            >>[/color][/color]
            neither ptr, nor the value pointed by ptr can change.
            [color=blue][color=green]
            >> please explain me the differences.[/color]
            >
            > Is this ==== char *const* ptr; ====== declaration valid ??
            >[/color]
            Yes, see above.

            By the way, did you try looking it up in some book before posting it
            here?

            ~seemanta

            Comment

            • Antonio Contreras

              #7
              Re: explanation needed on const pointers

              Seemanta Dutta wrote:[color=blue]
              > tatto0_2000@yah oo.com wrote:[color=green]
              > > deepunayak@gmai l.com wrote:[color=darkred]
              > >> I need complete explanation on constant pointers.
              > >>[/color][/color][/color]

              I think you need to study some more C before giving advice.
              [color=blue][color=green][color=darkred]
              > >> e.g
              > >>
              > >> char *const ptr ;[/color][/color]
              > value pointed by ptr is constant.
              > ptr can be changed.[/color]

              Just the other way arround. Here ptr is a constant pointer to a char,
              which means that the char it points to can be changed, but ptr itself
              cannot be changed.
              [color=blue][color=green][color=darkred]
              > >> const char *ptr ;[/color][/color]
              > value pointed by ptr can change.
              > ptr itself can't change.[/color]

              Again, the other way arround. Here ptr is a pointer to a constant char,
              which means that ptr can be changed but you cannot change the char it
              points to through this variable.
              [color=blue][color=green][color=darkred]
              > >> char *const* ptr;
              > >>[/color][/color]
              > neither ptr, nor the value pointed by ptr can change.[/color]

              Completely wrong. Here ptr is a pointer to a constant pointer to a
              char. This means that ptr can be changed. The pointer it points to
              cannot be changed, but the char pointed by the pointer pointed to by
              ptr can be changed.
              [color=blue][color=green][color=darkred]
              > >> please explain me the differences.[/color]
              > >
              > > Is this ==== char *const* ptr; ====== declaration valid ??
              > >[/color]
              > Yes, see above.
              >
              > By the way, did you try looking it up in some book before posting it
              > here?[/color]

              Did you try to looking it up before posting advice that was incorrect?

              Comment

              • stathis gotsis

                #8
                Re: explanation needed on const pointers

                <deepunayak@gma il.com> wrote in message
                news:1140074417 .452662.66880@g 43g2000cwa.goog legroups.com...[color=blue]
                > I need complete explanation on constant pointers.
                >[/color]

                My guesses would be the following.
                [color=blue]
                > char *const ptr ;[/color]

                constant pointer to char
                [color=blue]
                > const char *ptr ;[/color]

                pointer to constant char
                [color=blue]
                > char *const* ptr;[/color]

                pointer to constant pointer to char


                Comment

                • John Bode

                  #9
                  Re: explanation needed on const pointers


                  deepunayak@gmai l.com wrote:[color=blue]
                  > I need complete explanation on constant pointers.
                  >
                  > e.g
                  >
                  > char *const ptr ;[/color]

                  ptr may not be modified; *ptr may be modified:

                  *ptr = 'b'; /* allowed */
                  ptr = NULL; /* not allowed */
                  [color=blue]
                  > const char *ptr ;[/color]

                  ptr may be modified; *ptr may not be modified:

                  *ptr = 'b'; /* not allowed */
                  ptr = NULL; /* allowed */
                  [color=blue]
                  > char *const* ptr;[/color]

                  ptr may be modified; *ptr may not be modified; **ptr may be modified:

                  ptr = NULL; /* allowed */
                  *ptr = NULL; /* not allowed */
                  **ptr = 'a'; /* allowed */[color=blue]
                  >
                  > please explain me the differences.[/color]

                  Comment

                  • Christian Bau

                    #10
                    Re: explanation needed on const pointers

                    In article <1140074417.452 662.66880@g43g2 000cwa.googlegr oups.com>,
                    "deepunayak@gma il.com" <deepunayak@gma il.com> wrote:
                    [color=blue]
                    > I need complete explanation on constant pointers.
                    >
                    > e.g
                    >
                    > char *const ptr ;
                    > const char *ptr ;
                    > char *const* ptr;
                    >
                    > please explain me the differences.
                    >[/color]

                    Any good C reference will do.

                    Comment

                    Working...