Are addresses of const members const?

Guy DUBRISAY guy at hpgnd.grenoble.hp.com
Sat Feb 9 02:34:28 AEST 1991


'const' is a type modifier whose effect is to tell the compiler that an object
such declared cannot be written to (asigned to, incremented, decremented, ...).
Statements which are detected by the compiler to do so will cause an error.

Obviously any variable may be modified by 'const' argument whatever their type
is.

Beware of using 'const' modifier altogether with pointers :

const char * ptr ;	==> The pointer may be modified, but that which it
			    points to may not.

char * const ptr ;	==> The pointer may not be modified, but that which
			    it points to may.

const char * const ptr;	==> Neither the pointer nor that which it points to
			    may be modified.

Reading your code :

>const foo *aray1[] = { &item1, &item2 };
>const bar aray2[] = { &item1, &item2 }; 

... should NOT gives 'type mismatch' error!

> aray1[0] = &item3;

... is legal (modified pointer). This should cause an error if you had 
declared
		const foo * const aray1[] = { &item1, &item2 };

> aray2[0] = &item3;

... must produce an error such as "Cannot assign to a constant."

Hope this helps.

Guy.



More information about the Comp.lang.c mailing list