evaluate NULL to a pointer which a CONST pointer points to

Discussion in 'C++' started by G, Jan 8, 2007.

  1. G

    G Guest

    Hi~
    you guys.
    I come across the code below:
    /////////////////////////////////////////////////////////
    #include <stdio.h>
    void foo(const char **pp)
    {
    *pp=NULL;
    //it works too
    //*pp="Hello world!";
    }

    int main()
    {
    const char *p="hello";
    foo(&p);
    printf("%s",p);
    return 0;
    }
    //////////////////////////////////////////////////////////
    It was compiled successed without no warn or error.

    Why can *pp change , though " const char **pp " meant it couldn't ?

    Thank you ! :)
     
    G, Jan 8, 2007
    #1
    1. Advertising

  2. G

    G Guest

    Re: evaluate NULL to a pointer which a CONST pointer points to

    Well,I got the answer.
     
    G, Jan 8, 2007
    #2
    1. Advertising

  3. G

    G Guest

    Re: evaluate NULL to a pointer which a CONST pointer points to

    Well,I have got the answer.
     
    G, Jan 8, 2007
    #3
  4. Re: evaluate NULL to a pointer which a CONST pointer points to

    "G" <> wrote in message
    news:...
    > Well,I got the answer.


    Then post the answer, so others can benefit as well! :)

    So for the sake of completeness: you are allowed to change the pointer
    because the 'pointer to const char' that the pointer points to is itself not
    const. This may be a bit difficult to understand at first, so a few typedefs
    would clear things up:

    ----------
    typedef int the_type_pointed_to;

    void foo(the_type_pointed_to * ptr)
    {
    *ptr = 3; // allowed
    }

    void foo(the_type_pointed_to const * ptr)
    {
    *ptr = 4; // not allowed, the_type_pointed_to is const.
    }
    ----------

    This is of course very obvious. Now, change the_type_pointed_to to be a char
    const * rather than an int. You'll see that both overloads of foo are still
    different - one works on a pointer to nonconst 'char const *', while the
    other works on a pointer to _const_ 'char const *'. If you substitute
    the_type_pointed_to with the actual type in the second definition, it would
    read:
    void foo(char const * const * ptr)

    So it doesn't matter whether **ptr is const or when you want to change *ptr,
    it matters whether *ptr itself is const!

    - Sylvester
     
    Sylvester Hesp, Jan 8, 2007
    #4
    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. coala
    Replies:
    3
    Views:
    396
    coala
    Sep 6, 2006
  2. coala
    Replies:
    1
    Views:
    605
    Victor Bazarov
    Sep 6, 2006
  3. Javier
    Replies:
    2
    Views:
    600
    James Kanze
    Sep 4, 2007
  4. aneuryzma
    Replies:
    3
    Views:
    761
    Jim Langston
    Jun 16, 2008
  5. Disc Magnet
    Replies:
    1
    Views:
    645
    Ian Collins
    May 6, 2010
Loading...

Share This Page