evaluate NULL to a pointer which a CONST pointer points to

evaluate NULL to a pointer which a CONST pointer points to

Post by G » Tue, 09 Jan 2007 14:08:46


Well,I got the answer.
 
 
 

evaluate NULL to a pointer which a CONST pointer points to

Post by G » Tue, 09 Jan 2007 14:09:31

Well,I have got the answer.

 
 
 

evaluate NULL to a pointer which a CONST pointer points to

Post by Sylvester » Wed, 10 Jan 2007 01:22:38


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