const declaration 
Author Message
 const declaration

I've been trying to understand the const declaration more clearly. I think
I understand that 'const char * S' means I can change the pointer which S is
assigned to, but I can't change the contents of what S points at. And
conversely 'char * const S' means I can change the contents of what S
points at, but I can't change the pointer S is assigned to. And
'const char * const S' means I can't change nuthin'. Am I correct so far?

One question is if there is any difference between 'const char *' and
'char const *'. As far as I can tell, my compiler behaves the same given
those two different declarations.

And secondly I was wondering if someone could expound on the implications
when KNR2 on p. 40 says "The result is implementation-defined if an attempt
is made to change a const." My compiler errors when that is attempted, but
can I assume there are compilers that do just about anything: nothing,
allow the assignment but give weirdo results, allow the assignment and it
works, ...?

Thanks a lot,
tim ayers                        ADS Communications



Tue, 19 Aug 1997 06:48:11 GMT  
 const declaration

writes:
<snipped questions about const syntax>
Yes on all counts. `const char *' and `char const *' are `pointer to const
char' and  `char *const' is `const pointer to char'. The general rule is to
read outward (leftward) except that const at end applies to the previous
token etc.

|> And secondly I was wondering if someone could expound on the implications
|> when KNR2 on p. 40 says "The result is implementation-defined if an attempt
|> is made to change a const." My compiler errors when that is attempted, but
|> can I assume there are compilers that do just about anything: nothing,
|> allow the assignment but give weirdo results, allow the assignment and it
|> works, ...?

If an attempt is made to change an object through a const lvalue, a
diagnostic _must_ be issued. However, it is possible to be {*filter*} and try to
change a const object through an lvalue which is not const: sometimes the
nastiness may be too great for the compiler. Consider (in a function, and
after inclusion of <string.h>):

const char a[] = "Hello World";
char *b = strchr(a,a[0]);
*b = 'G';

This is what brings in undefined behaviour: a[0] was _defined_ `const char',
*b is the same object as a[0], and yet, *b has type `char', and *b is being
modified. The compiler is not required to give any diagnostic, though I would
love a compiler which knew enough about strchr to diagnose this. (Note that
nowhere does this example have a cast! It is an indication of inconsistent
treatment of `const's in ANSI C, but I can't think of a better way within the
generic framework).

Note that the following is _not_ undefined behaviour:

char a[] = "Hello World";
const char *b = a;
char *c = strchr(b,b[0]);
*c = 'G';

The difference is that *c (of type `char'), though the same as *b (of type
`const char') nevertheless was not _defined_ as a const char. (The const char
*b line _defined_, i,e, allocated space for, b, but not *b). I consider it as
bad programming though, and I would like a compiler to diagnose this as well.
(This also shows why the compiler, in the general case, can only give a
non-fatal warning, if it decides to warn at all).

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
< http://www.*-*-*.com/ ;or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- < http://www.*-*-*.com/ ;
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Tue, 19 Aug 1997 07:44:44 GMT  
 const declaration

Quote:

> I've been trying to understand the const declaration more clearly. I think
> I understand that 'const char * S' means I can change the pointer which S is
> assigned to, but I can't change the contents of what S points at. And
> conversely 'char * const S' means I can change the contents of what S
> points at, but I can't change the pointer S is assigned to. And
> 'const char * const S' means I can't change nuthin'. Am I correct so far?

Right.

Quote:
> One question is if there is any difference between 'const char *' and
> 'char const *'. As far as I can tell, my compiler behaves the same given
> those two different declarations.

No difference.  Either is valid according to the standard.

Quote:
> And secondly I was wondering if someone could expound on the implications
> when KNR2 on p. 40 says "The result is implementation-defined if an attempt
> is made to change a const." My compiler errors when that is attempted, but
> can I assume there are compilers that do just about anything: nothing,
> allow the assignment but give weirdo results, allow the assignment and it
> works, ...?

Any compiler should issue a diagnostic if you try to change a const object
directly.  However, it is legal in some cases to use a cast to get around
the constness.  This will work if the object is not really const.  For
example,

        void foo(const char* s)
        {
          if (s != NULL)
            * (char*) s = 'x';
        }

        char ar[] = "not const";
        char c_ar[] = "const";

        void bar()
        {
          foo(ar);      /* no problem -- object is not really const */
          foo(c_ar);    /* may be a problem -- foo will remove const from */
                        /* a const object */
        }

the first call to foo works fine, but the second results in undefined
behavior (ISO 6.5.3).  The most likely problem is that c_ar may be put in
readonly memory and attempting to store something in it may result in
a runtime error.  The standard, however, allows anything to happen.

I can't find anything in the standard that indicates that anything like this
is implementation defined.  The compiler is required to do something sensible
and document implementation defined behavior -- it is not required to do
either for undefined behavior.

--
Mike Rubenstein



Sun, 24 Aug 1997 05:37:21 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. const declarations in ANSI C

2. question on const declaration

3. extern const declaration

4. const declarations and arrays

5. const declarations and arrays

6. How tightly does const bind within declaration?

7. const convention in func prototypes/declarations

8. CONST after a function declaration

9. Const on a data declaration

10. Declaration of static const types

11. const after function declaration?

12. Help: inline char const* const& max(char const* const &a, char const* const &b)

 

 
Powered by phpBB® Forum Software