Incorrect Generation of C2166 Using const keyword in C 5.10

ID Number: Q48723

5.10 | 5.10

MS-DOS | OS/2

buglist5.10 fixlist6.00

Summary:

There are some cases where the Microsoft C Compiler version 5.10

incorrectly implements the const keyword. The sample code below

illustrates this. When the code is compiled, the compiler incorrectly

generates the following error message:

error C2166: lval specifies 'const' object

The following error message should be given; it references a different

line:

error C2166: lval specifies 'const' object

Microsoft has confirmed this to be a problem in C version 5.10 and

QuickC versions 2.00 and 2.01 (buglist2.00 and buglist2.01). This

problem was corrected in C version 6.00 and QuickC versions 2.50 and

2.51 (fixlist2.50 and fixlist2.51).

More Information:

The first parameter to the function is a pointer to a constant string.

Thus, the pointer itself may be modified but the string may not be.

Therefore, since the fourth line is attempting to modify the contents

of the string, an error message should be generated.

The second parameter to the function is a constant pointer to a

string. Thus, the pointer value may not be modified, but the contents

of the string may be modified. Since the fifth line does not attempt

to modify the pointer to the string, an error message should not be

generated.

Sample Code

-----------

void foo(char const *string1, char * const string2)

{

int k = 1;

*(string1 + k) = 'B'; /* should give error */

*(string2 + k) = 'Y'; /* incorrectly gives error */

}