By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,505 Members | 1,168 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,505 IT Pros & Developers. It's quick & easy.

Question on const pointer modifier

P: n/a
I was a bit surprised when my compiler (both GCC 3.2 & MSVC 7.1) griped
about this piece of code:

int i;
int *p1 = &i;
const int **p2 = &p1; // Invalid type conversion here

I was curious as to why this is so. I don't understand how it would be wrong
to convert an int ** into a const int **. Thanks in advance!

-Matt
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Matt Taylor" <pa**@tampabay.rr.com> wrote...
I was a bit surprised when my compiler (both GCC 3.2 & MSVC 7.1) griped
about this piece of code:

int i;
int *p1 = &i;
const int **p2 = &p1; // Invalid type conversion here

I was curious as to why this is so. I don't understand how it would be wrong to convert an int ** into a const int **. Thanks in advance!


Read C++ FAQ. The one you need is 18.15.

Victor
Jul 22 '05 #2

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:2wjPb.108895$8H.221197@attbi_s03...
"Matt Taylor" <pa**@tampabay.rr.com> wrote...
I was a bit surprised when my compiler (both GCC 3.2 & MSVC 7.1) griped
about this piece of code:

int i;
int *p1 = &i;
const int **p2 = &p1; // Invalid type conversion here

I was curious as to why this is so. I don't understand how it would be

wrong
to convert an int ** into a const int **. Thanks in advance!


Read C++ FAQ. The one you need is 18.15.


Ah now I understand. Thanks!

-Matt
Jul 22 '05 #3

P: n/a
"Matt Taylor" <pa**@tampabay.rr.com> wrote in message
news:0q******************@twister.tampabay.rr.com
I was a bit surprised when my compiler (both GCC 3.2 & MSVC 7.1)
griped about this piece of code:

int i;
int *p1 = &i;
const int **p2 = &p1; // Invalid type conversion here

I was curious as to why this is so. I don't understand how it would
be wrong to convert an int ** into a const int **. Thanks in advance!

-Matt

This is a non-obvious language restriction designed to stop people from
getting around const specifiers by using indirection. In your example, i is
not const, so presumably there is no objection to it being modified by using
indirection. Nevertheless, the language restriction is motivated by concerns
about modifying const objects.

Suppose that there was an automatic conversion from int ** to const int **
and suppose that there was another variable j, which was a const int.

const int j=2;

int i;
int *p1 = &i;
const int ** p2 = &p1;

Consider p2. Since it is typed as a "pointer to a pointer to const int",
dereferencing it must give the type "pointer to const int". Accordingly,
there is no conversion required in

*p2 = &j;

But p2 points to p1, so this last line has the effect of assigning the
address of j to p1, i.e., it has the effect of

p1 = &j;

Now, since p1 is a pointer to non-const int, the following must be legal

*p1 = 5;

Since p1 points to j, this last line has the effect of assigning 5 to j,
i.e.,

j = 5;

Thus a const object has been modified by using indirection.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #4

P: n/a
"John Carson" <do***********@datafast.net.au> wrote...
"Matt Taylor" <pa**@tampabay.rr.com> wrote in message
news:0q******************@twister.tampabay.rr.com
I was a bit surprised when my compiler (both GCC 3.2 & MSVC 7.1)
griped about this piece of code:

int i;
int *p1 = &i;
const int **p2 = &p1; // Invalid type conversion here

I was curious as to why this is so. I don't understand how it would
be wrong to convert an int ** into a const int **. Thanks in advance!

-Matt

This is a non-obvious language restriction [...]


You might want to check out FAQ 5.5 and 5.6.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.