473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

conversion between pointer_to_const int and pointer_to_int

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

Sep 8 '06 #1
4 1783
pauldepst...@att.net wrote:
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.

Sep 8 '06 #2
pa**********@att.net wrote:
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.
>
Thank you for your explanation.

Paul Epstein
Sep 8 '06 #3
pa**********@att.net wrote:
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-lit...html#faq-18.16

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 8 '06 #4
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++.

--

Frederick Gotham
Sep 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
0
by: Lou Evart | last post by:
DOCUMENT CONVERSION SERVICES Softline International (SII) operates one of the industry's largest document and data conversion service bureaus. In the past year, SII converted over a million...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.