explanation needed on const pointers

Discussion in 'C Programming' started by deepunayak@gmail.com, Feb 16, 2006.

  1. Guest

    I need complete explanation on constant pointers.

    e.g

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

    please explain me the differences.
     
    , Feb 16, 2006
    #1
    1. Advertising

  2. Sreekanth Guest

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

    http://www.geocities.com/spur4444/prog/const_and_pointers.html


    wrote:
    > I need complete explanation on constant pointers.
    >
    > e.g
    >
    > char *const ptr ;
    > const char *ptr ;
    > char *const* ptr;
    >
    > please explain me the differences.
     
    Sreekanth, Feb 16, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > I need complete explanation on constant pointers.
    >
    > e.g
    >
    > char *const ptr ;
    > const char *ptr ;
    > char *const* ptr;
    >
    > please explain me the differences.


    Is this ==== char *const* ptr; ====== declaration valid ??
     
    , Feb 16, 2006
    #3
  4. Pedro Graca Guest

    wrote:
    > I need complete explanation on constant pointers.
    >
    > e.g
    >
    > char *const ptr ;


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

    > const char *ptr ;


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

    > char *const* ptr;


    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>
     
    Pedro Graca, Feb 16, 2006
    #4
  5. On Wed, 15 Feb 2006 23:20:17 -0800, wrote:

    > I need complete explanation on constant pointers.


    Ah, you'll need a good book for that.

    > char *const ptr ;
    > const char *ptr ;
    > char *const* ptr;
    >
    > please explain me the differences.


    If you use the rule I outlined in:

    http://groups.google.co.uk/group/co...9f1cd41f?lnk=st&rnum=1&hl=en#e44e3d249f1cd41f

    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.
     
    Ben Bacarisse, Feb 16, 2006
    #5
  6. wrote:
    > wrote:
    >> I need complete explanation on constant pointers.
    >>
    >> e.g
    >>
    >> char *const ptr ;

    value pointed by ptr is constant.
    ptr can be changed.

    >> const char *ptr ;

    value pointed by ptr can change.
    ptr itself can't change.
    >> char *const* ptr;
    >>

    neither ptr, nor the value pointed by ptr can change.

    >> please explain me the differences.

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

    Yes, see above.

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

    ~seemanta
     
    Seemanta Dutta, Feb 16, 2006
    #6
  7. Seemanta Dutta wrote:
    > wrote:
    > > wrote:
    > >> I need complete explanation on constant pointers.
    > >>


    I think you need to study some more C before giving advice.

    > >> e.g
    > >>
    > >> char *const ptr ;

    > value pointed by ptr is constant.
    > ptr can be changed.


    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.

    > >> const char *ptr ;

    > value pointed by ptr can change.
    > ptr itself can't change.


    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.

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

    > neither ptr, nor the value pointed by ptr can change.


    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.

    > >> please explain me the differences.

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

    > Yes, see above.
    >
    > By the way, did you try looking it up in some book before posting it
    > here?


    Did you try to looking it up before posting advice that was incorrect?
     
    Antonio Contreras, Feb 16, 2006
    #7
  8. <> wrote in message
    news:...
    > I need complete explanation on constant pointers.
    >


    My guesses would be the following.

    > char *const ptr ;


    constant pointer to char

    > const char *ptr ;


    pointer to constant char

    > char *const* ptr;


    pointer to constant pointer to char
     
    stathis gotsis, Feb 16, 2006
    #8
  9. John Bode Guest

    wrote:
    > I need complete explanation on constant pointers.
    >
    > e.g
    >
    > char *const ptr ;


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

    *ptr = 'b'; /* allowed */
    ptr = NULL; /* not allowed */

    > const char *ptr ;


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

    *ptr = 'b'; /* not allowed */
    ptr = NULL; /* allowed */

    > char *const* ptr;


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

    ptr = NULL; /* allowed */
    *ptr = NULL; /* not allowed */
    **ptr = 'a'; /* allowed */
    >
    > please explain me the differences.
     
    John Bode, Feb 16, 2006
    #9
  10. In article <>,
    "" <> wrote:

    > I need complete explanation on constant pointers.
    >
    > e.g
    >
    > char *const ptr ;
    > const char *ptr ;
    > char *const* ptr;
    >
    > please explain me the differences.
    >


    Any good C reference will do.
     
    Christian Bau, Feb 16, 2006
    #10
    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. Replies:
    11
    Views:
    1,151
  2. Javier
    Replies:
    2
    Views:
    621
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,171
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    944
    fungus
    Oct 31, 2008
  5. Poster Matt
    Replies:
    21
    Views:
    1,753
    Poster Matt
    Jun 16, 2010
Loading...

Share This Page