Use of Const keyword in ANSI C

Richard A. O'Keefe ok at quintus.uucp
Thu Sep 8 19:45:34 AEST 1988


In article <441 at optilink.UUCP> cramer at optilink.UUCP (Clayton Cramer) writes:
>I'm starting to use the 'const' keyword with the Microsoft V5.1 C compiler.
>I've found something that may be a bug, or it may be that I don't 
>adequately understand 'const'.
>I've defined a structure:
>    typedef struct Tag { ... const char* C; } TestType;
>    TestType Test1 = ...;
>    Test1.C = "not a test case";    /* compiler accepts this -- bad */

The compiler is right: const char *C is to be read *backwards*:
C is a pointer to readonly characters.  If you want C to be constant, say
	char * const C;		/* C is a constant pointer to char */
If you want *both* C and what it points to to be readonly, say
	const char * const C;

The "cdecl" utility -- available from comp.sources archives -- is a big help!



More information about the Comp.lang.c mailing list