Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > evaluate NULL to a pointer which a CONST pointer points to

Reply
Thread Tools

evaluate NULL to a pointer which a CONST pointer points to

 
 
G
Guest
Posts: n/a
 
      01-08-2007
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 !

 
Reply With Quote
 
 
 
 
G
Guest
Posts: n/a
 
      01-08-2007
Well,I got the answer.

 
Reply With Quote
 
 
 
 
G
Guest
Posts: n/a
 
      01-08-2007
Well,I have got the answer.

 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-08-2007
"G" <> wrote in message
news: ps.com...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const pointer versus const content pointed to by pointer Disc Magnet C Programming 1 05-06-2010 10:27 AM
Null pointer (NULL array pointer is passed) aneuryzma C++ 3 06-16-2008 05:48 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 1 09-06-2006 03:00 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 3 09-06-2006 02:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57