Use of Const keyword in ANSI C

Chris Torek chris at mimsy.UUCP
Fri Sep 9 04:59:58 AEST 1988


In article <441 at optilink.UUCP> cramer at optilink.UUCP (Clayton Cramer) writes:
[structure containing]
>        const char*     C;
>
>    Test1.C = "not a test case";    /* compiler accepts this -- bad */
>
>Should the attempt to set Test1.C to point to another string cause a
>complaint from the compiler? ... [or] have I missed something here in
>my understanding of 'const' ... ?

The latter: `const' applies here to the characters, not to the pointer.
So `Test1.C[n] = expr' is illegal, but `Test1.C = expr' is legal.  If
you just want the pointer to be constant, use

	char *const C;

and if you want both constant, use

	const char *const C;

The `volatile' modifier works in the same fashion.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list