473,394 Members | 1,854 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,394 software developers and data experts.

Stroustrup 5.4.1, pointers and constants

int a = 1;
const int c = 2;

now Stroustrup declares pointers for "a" and "c":

const int* p1 = &c; // ok
const int* p2 = &a; // he says it is OK

i don't get it, "a" is not a constant and we are declaring a "pointer
to a constant" . what is this ?

after that, is this legal:

int* p3 = &a;

?

Mar 28 '07 #1
7 1608
* arnuld:
int a = 1;
const int c = 2;

now Stroustrup declares pointers for "a" and "c":

const int* p1 = &c; // ok
const int* p2 = &a; // he says it is OK

i don't get it, "a" is not a constant and we are declaring a "pointer
to a constant" . what is this ?
"pointer to a constant" is a restriction on how that pointer can be
used. p2 cannot be used to change a. It does not guarantee that a
isn't changed by other means, it only guarantees that given p2, and
using only p2, you can't change a.

after that, is this legal:

int* p3 = &a;

?
Yes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 28 '07 #2
On Mar 28, 12:18 pm, "Alf P. Steinbach" <a...@start.nowrote:
* arnuld:
[SNIP]
"pointer to a constant" is a restriction on how that pointer can be
used. p2 cannot be used to change a. It does not guarantee that a
isn't changed by other means, it only guarantees that given p2, and
using only p2, you can't change a.
then what about this:

const int* p2 = &a;

you said i can not change "a" using p2 but i can using some other
pointer. then

int *const p22 = &a;

this means, "a" will *never* be changed. ... right ?

after that, is this legal:
int* p3 = &a;
?

Yes.
*p3 = 78;

and "a" will get changed to value 78 but i can not use "p3" on:

int *const p22 = &a;

then what does this mean:

const int *const p4 = &a;

?

Mar 28 '07 #3
* arnuld:
>On Mar 28, 12:18 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* arnuld:
>[SNIP]
>"pointer to a constant" is a restriction on how that pointer can be
used. p2 cannot be used to change a. It does not guarantee that a
isn't changed by other means, it only guarantees that given p2, and
using only p2, you can't change a.

then what about this:

const int* p2 = &a;

you said i can not change "a" using p2 but i can using some other
pointer. then

int *const p22 = &a;

this means, "a" will *never* be changed. ... right ?
No, it means p22 will never be changed.

Reading the declaration backwards, p22 is a constant pointer to int.

Since p22 is constant it cannot be changed; since it's a pointer to
non-constant it can be used to change whatever it points to.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 28 '07 #4
On Mar 28, 12:48 pm, "Alf P. Steinbach" <a...@start.nowrote:
* arnuld:
int *const p22 = &a;
this means, "a" will *never* be changed. ... right ?

No, it means p22 will never be changed.

Reading the declaration backwards, p22 is a constant pointer to int.

Since p22 is constant it cannot be changed; since it's a pointer to
non-constant it can be used to change whatever it points to.
ok i will try one more time:

int a = 1;

const int* p = &a;

it is a pointer to a constant, whereas neither "p" nor "a" is not a
constant. it only means can not change "a" using "p" but "some other
pointer" can and we can also make "p" to point some where else.
int *const p1 = &a;

"constant pointer" to a variable. we can not change the "p1" to point
to anything else. we can change the value of "a" using "p1" or some
other pointer.

const int *const p2 = &a;

we can not change anything here.

Mar 28 '07 #5
Alf P. Steinbach wrote:
it only guarantees that given p2, and
using only p2, you can't change a.
Technically speaking, that's not actually true: const_cast<>
Mar 28 '07 #6
* arnuld:
>On Mar 28, 12:48 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* arnuld:
>>int *const p22 = &a;
this means, "a" will *never* be changed. ... right ?
No, it means p22 will never be changed.

Reading the declaration backwards, p22 is a constant pointer to int.

Since p22 is constant it cannot be changed; since it's a pointer to
non-constant it can be used to change whatever it points to.

ok i will try one more time:

int a = 1;

const int* p = &a;

it is a pointer to a constant, whereas neither "p" nor "a" is not a
constant. it only means can not change "a" using "p" but "some other
pointer" can and we can also make "p" to point some where else.
int *const p1 = &a;

"constant pointer" to a variable. we can not change the "p1" to point
to anything else. we can change the value of "a" using "p1" or some
other pointer.

const int *const p2 = &a;

we can not change anything here.
Yep.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 28 '07 #7
* Juha Nieminen:
Alf P. Steinbach wrote:
>it only guarantees that given p2, and
using only p2, you can't change a.

Technically speaking, that's not actually true: const_cast<>
Technically speaking there are at least 101 more ways to circumvent the
type safety.

But don't list them, please.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 28 '07 #8

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

Similar topics

10
by: DevarajA | last post by:
I've read that 0 is the 'null pointer constant', and assigning it to a pointer makes it a 'null pointer'. Is that true? And is the opposite true? (assigning a null pointer to an int sets the int to...
46
by: TTroy | last post by:
Hi, I'm just wondering why people/books/experts say "the function returns a pointer to.." or "we have to send scanf a pointer to.." instead of "the function returns the address of.." or "we have...
9
by: arnuld | last post by:
problem: define a /struct Date/ to keep track of dates. provide functions that read Dates from input, write Dates to output & initialize a date with date. solution: i thought of a /vector/ of...
4
by: arnuld | last post by:
here Stroustrup asks to write the smallest and largest values of char, int, short, long etc. i do not understand his question. when i will do sizeof(char), i wil only get one value in /byte/....
16
by: arnuld | last post by:
i did what i could do at Best to solve this exercise and this i what i have come up with: ----------- PROGRAMME -------------- /* Stroustrup 5.9, exercise 3 STATEMENT: Use typedef to define...
11
by: arnuld | last post by:
this programme compiles but runs into some strange results, a semantic- BUG is there. i even put some "std::cout" to know what is wrong but it does something that i did NOT order it to do: ...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
8
by: fauxanadu | last post by:
Hello: I've been working on an assignment and I have hit a stumbling block pertaining to an array of pointers. If I am correct in my assumptions, the array struct animal* zoo should have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.