const and volatile

David Howard dhoward at ready.eng.ready.com
Sat May 18 07:39:23 AEST 1991


Questions on const and volatile:

I am using the latest release of the Microtec ANSI C cross compiler, PC hosted,
680x0 target. I am attempting to use const and volatile and getting
results that are slightly different from what I expect. If anyone out ther
has a comment, please do:

const
-----

I declare:

const unsigned char *device = (unsigned char *)0x600000b;

Then I do:

*device = 0x01;

The compiler complains:

'attempt to modify a const'

what I want is for the pointer to be const, but not what it points to!
Basically, I want all my 'consts' in ROM so they won't get trashed, but
I wish to access variable data thru them. I tried various combinations
of parentheses and got nowhere. By the way, the compiler also complains
if I do:

device = (unsigned char *)0x600000d;

but that is what I expect. It doesn't seem right that the declaration should
make BOTH the pointer and the pointee const.

likewise with volatile, I get a reverse situation. I want to point at
a device register that is *really* volatile. In this case I want the 
pointee to be volatile but not the pointer. If I declare the 
pointer:

volatile unsigned char *device = 0x600000e;

the compiler treats BOTH the pointer and pointee as volatile. By that
I mean, when I use the pointer, the compiler always reloads the pointer
itself, and also always reaccesses the dereferenced location. In this
case it works OK, but generates extra code to reload the pointer with
0x600000e every time I use it.

It appears that since the specification of const and volatile is implementation
dependent, the Microtec guys intepreted it in a strange way.            

I guess what I really want is the pointer to be const, and the pointee to
be volatile. Anyone have a suggestion on how to declare the pointer
to get that result? What do other ANSI compilers do?



More information about the Comp.lang.c mailing list