Use of Const keyword in ANSI C

Clayton Cramer cramer at optilink.UUCP
Thu Sep 8 09:39:34 AEST 1988


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 int       A;
        short           B;
        const char*     C;
        } TestType;

    TestType        Test1 = {10, 10, "test case"};

    Test1.A = 5;            /* compiler complains about this -- good */
    Test1.B = 20;           /* compiler accepts this -- fine */
    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?

If I redefine the structure the same as above but with:

    const char          C[20];

and a statement:

    Test1.C[0] = '0';

the compiler complains (correctly) "lval specifies 'const' object".

So have I missed something here in my understanding of 'const', or
is the Microsoft compiler broke?

Clayton E. Cramer
..!hplabs!pyramid!kontron!optilin!cramer



More information about the Comp.lang.c mailing list