conversion between pointer_to_const int and pointer_to_int

P

pauldepstein

I wrote the following code in an attempt to understand the type
conversion in the subject title:

#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int ci = 0;
const int *p = &ci;
cout << p << endl;
ci = 1;
ci = 2;
cout << "pointer_type"<< typeid(p).name()<< endl
<< "ci_value" << ci << endl << p << endl << *p
<< "ok, well if p is a const int* why id you allow me to change
ci????";

cin.get();
}

My question is apparent. I was surprised by the result: I expected
some type of error message.
Since p is a pointer_to_const, why is it ok to change the value lying
in the address that p points to? Doesn't that contradict the whole
pointer_to_const concept? (I had the (apparently false) belief that the
const int* p definition-and-declaration would convert ci to a const.)

Thank you for your explanation.

Paul Epstein
 
M

Michiel.Salters

I wrote the following code in an attempt to understand the type
conversion in the subject title:

#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int ci = 0;
const int *p = &ci;
cout << p << endl;
ci = 1;
ci = 2;
cout << "pointer_type"<< typeid(p).name()<< endl
<< "ci_value" << ci << endl << p << endl << *p
<< "ok, well if p is a const int* why id you allow me to change
ci????";

cin.get();
}

My question is apparent. I was surprised by the result: I expected
some type of error message.
Since p is a pointer_to_const, why is it ok to change the value lying
in the address that p points to?

Because the const applies only to p. It means you cannot change ci
through *p. The const-ness of *p doesn't change during execution, nor
does the non-const-ness of ci. Did you expect ci to become non-const
again if you executed p = 0; ?

Objects in C++ never change type. However, pointers can be changed
to point to objects of different types (or be 0, and not point to
anything
at all). E.g. an int const* can start out pointing to an int, and later
point
to an int const.

Regards,
Michiel Salters.
 
C

Carlos Martinez

I wrote the following code in an attempt to understand the type
conversion in the subject title:

#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int ci = 0;
const int *p = &ci;
cout << p << endl;
ci = 1;
ci = 2;
cout << "pointer_type"<< typeid(p).name()<< endl
<< "ci_value" << ci << endl << p << endl << *p
<< "ok, well if p is a const int* why id you allow me to change
ci????";

cin.get();
}

My question is apparent. I was surprised by the result: I expected
some type of error message.
Since p is a pointer_to_const, why is it ok to change the value lying
in the address that p points to? Doesn't that contradict the whole
pointer_to_const concept? (I had the (apparently false) belief that the
const int* p definition-and-declaration would convert ci to a const.)

p, as pointer_to_const_int, cannot be used to change value pointed to,
but this doesn't mean you cannot change the same value throug another
variable (ci in your code).

In other words:
data is not const or not const. What is const or not const is variable
used to access data.
 
M

Marcus Kwok

I wrote the following code in an attempt to understand the type
conversion in the subject title:

#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int ci = 0;
const int *p = &ci;
cout << p << endl;
ci = 1;
ci = 2;
cout << "pointer_type"<< typeid(p).name()<< endl
<< "ci_value" << ci << endl << p << endl << *p
<< "ok, well if p is a const int* why id you allow me to change
ci????";

cin.get();
}

My question is apparent. I was surprised by the result: I expected
some type of error message.
Since p is a pointer_to_const, why is it ok to change the value lying
in the address that p points to? Doesn't that contradict the whole
pointer_to_const concept? (I had the (apparently false) belief that the
const int* p definition-and-declaration would convert ci to a const.)

See this FAQ:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.16
 
F

Frederick Gotham

int main()
{
int ci = 0;
const int *p = &ci;
cout << p << endl;
ci = 1;
ci = 2;
cout << "pointer_type"<< typeid(p).name()<< endl
<< "ci_value" << ci << endl << p << endl << *p
<< "ok, well if p is a const int* why id you allow me to change
ci????";


"ci" is a non-const int.

"p" is a non-const pointer to a const int.

These two facts are indepedant of one another.

By defining a pointer as a "pointer to const", you indicate that the
pointer shall _not_ be used to alter the data at that address. Therefore,
the following shall _not_ compile:

int i = 0;

int const *p = &i;

*p = 5;

, whereas the following, _will_ compile:

int i = 0;

int const *p = &i;

i = 5;
My question is apparent. I was surprised by the result: I expected
some type of error message.
Since p is a pointer_to_const, why is it ok to change the value lying
in the address that p points to?


Because you don't use the pointer to do so.

Doesn't that contradict the whole
pointer_to_const concept?


No, the "pointer to const" concept exists so that a particular pointer may
_not_ be used to alter data at that address.

(I had the (apparently false) belief that the
const int* p definition-and-declaration would convert ci to a const.)


Well now you know better.

As an aside, the following is a bit dubious:

int i = 0;

int const *restrict p = &i;

i = 5;

The "restrict" concept is a part of C99, although it is not currently a
part of C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top