Use of Const keyword in ANSI C

William E. Davidsen Jr davidsen at steinmetz.ge.com
Fri Sep 9 04:37:00 AEST 1988


In article <441 at optilink.UUCP> cramer at optilink.UUCP (Clayton Cramer) writes:

| 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 */

  I think it's right. You have a "pointer to const char" (I think) and a
quoted string is (can be?) a const string, so it works. After the first
assignment try
	Test1.C[2] = 'x';
and see if it tells you you can't modify a const string.
-- 
	bill davidsen		(wedu at ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me



More information about the Comp.lang.c mailing list