|
From: Juliano <jp...@bo...> - 2002-06-14 20:35:49
|
Oh God... :). I think its clear now... At least for me. Thank you. Juliano -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Mac Reiter Sent: sexta-feira, 14 de junho de 2002 16:55 To: 'Dev-C++ Mailing List' Subject: RE: [Dev-C++] const modifier? Quer ter seu pr=F3prio endere=E7o na Internet? Garanta j=E1 o seu e ainda ganhe cinco e-mails personalizados. = Dom=EDniosBOL - http://dominios.bol.com.br 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 =3D=3D> (const char) (*) y =3D=3D> 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 =3D=3D> (char) (const *) y =3D=3D> 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 =3D=3D> (char *) (const y) =3D=3D> 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 =3D=3D> (const char *) (y) char const *y =3D=3D> (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 =3D=3D> (const char) * (y) =3D=3D> chars are const, y is = mutable char const * y =3D=3D> (char const) * (y) =3D=3D> chars are const, y is = mutable char * const y =3D=3D> (char) * (const y) =3D=3D> chars are mutable, y = is const const char * const y =3D=3D> (const char) * (const y) =3D=3D> chars are const, y is const const char const * const y =3D=3D> (const char const) * (const y) = =3D=3D> 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[] =3D "Hello"; char buffer2[] =3D "World"; // This declares a mutable pointer that points to constant data const char * a =3D &(buffer1[0]); // This declares a constant pointer that points to mutable data char * const b =3D &(buffer1[0]); a[1] =3D 'u'; // illegal, because "mutable a" points to "const char" b[1] =3D 'u'; // OK, because "const b" is a mutable "char *" a =3D &(buffer2[0]); // OK, because "mutable a" can change b =3D &(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=20 >the address value stored in the pointer "y" can't be changed? In other=20 >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.) =20 _______________________________________________________________ Don't miss the 2002 Sprint PCS Application Developer's Conference August 25-28 in Las Vegas - http://devcon.sprintpcs.com/adp/index.cfm?source=3Dosdntextlink _______________________________________________ Dev-cpp-users mailing list Dev...@li... https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |