|
From: Mac R. <re...@no...> - 2002-06-14 19:57:09
|
I must apologize for _part_ of my preceding post, and should remember to
always run programs to make sure I'm not off slightly. I was still right
about the particular case raised by Juliano:
const char *y ==> (const char) (*) y ==> y is mutable pointer to const char
(mutable just means non-const) This means that "const char *y" will allow
you to change *where* y points, but will not allow you to change the data
that y is pointing at.
If you read my previous post, you would think that what you want is:
(WARNING : the following decomposition is incorrect!)
char const *y ==> (char) (const *) y ==> y is const pointer to mutable char
However, this is *WRONG*. "char const *y" is functionally identical to
"const char *y". (I really hate this syntax...) I discovered this while
writing a test program. To make the pointer constant, but the characters
mutable, you need:
char * const y ==> (char *) (const y) ==> y is a const pointer to mutable char
(I hate this syntax. I hate this syntax. I hate this syntax.)
Apparently C++ parses these two declarations identically:
const char *y ==> (const char *) (y)
char const *y ==> (char const *) (y)
-------------------------------------------------------------------------
THE MORAL OF THE STORY:
Split pointer declarations at the '*'. Then you can tell where the
const will be applied.
const char * y ==> (const char) * (y) ==> chars are const, y is mutable
char const * y ==> (char const) * (y) ==> chars are const, y is mutable
char * const y ==> (char) * (const y) ==> chars are mutable, y is const
const char * const y ==> (const char) * (const y) ==>
chars are const, y is const
const char const * const y ==> (const char const) * (const y) ==>
warning: const specifier used twice
-------------------------------------------------------------------------
Here is the test code -- put it in a file and compile it. The compiler
will throw an error on the two lines where a constant value is being
changed (or trying to be changed...)
int main(int argc, char **argv)
{
char buffer1[] = "Hello";
char buffer2[] = "World";
// This declares a mutable pointer that points to constant data
const char * a = &(buffer1[0]);
// This declares a constant pointer that points to mutable data
char * const b = &(buffer1[0]);
a[1] = 'u'; // illegal, because "mutable a" points to "const char"
b[1] = 'u'; // OK, because "const b" is a mutable "char *"
a = &(buffer2[0]); // OK, because "mutable a" can change
b = &(buffer2[0]); // illegal, because "const b" cannot change
}
Anybody care to join me in my mantra?
I HATE THIS SYNTAX!
I Hate This Syntax!
i hate this syntax
...
..
.
Mac
At 04:10 PM 6/14/02 -0300, Juliano wrote:
> I'm sorry if I'm wrong, but... Isn't "const char *y" a way to
>say that the address value stored in the pointer "y" can't be changed?
>In other words, I can change the value pointed by the address stored in
>"y", but I can't change the address stored in "y" itself.
>
> Isn't it the way pointers work? Am I doing something wrong?
>
> []s
> Juliano
[My previous post clipped out because it contained incorrect information,
which I choose not to propagate any further... Mac]
-
_____________________________ /"\
Mac Reiter \ / ASCII Ribbon Campaign
Nomadics, Inc. X Against HTML Mail
re...@no... / \ (To join the campaign, simply use
this in your signature.)
|