Electronic – PIC warning 364 related to initialization of const

embeddedhi-tech-compilermicrocontrollermplabpic

There are no answers to this on the internet that I could find, and I've looked twice over the last 4 months.

In MPLab v8.88 using the Hi-Tech ANSI C Compiler, I have this line of code:

const   uint8 SUM_THRESHOLD_MIN = 15;   /* comment edited out   */

and I get the warning:

Warning [364] C:\*directory edited out* \ *filename_edited_out*.c; 273.35 attempt to modify object qualifed const

(excuse the edits, but I felt I should edit out personal yet superfluous details).

It is not my code, and I'd just use a #define, but others want to use a const (for those that don't know: using a const guarantees a proper typecast of a value, and can save you from some weird issues related to typecasting and data types; it's not my favorite way of doing it, but it's not a bad idea either).

There are several const initializations happening in the same block of code, and they all give me this warning. They are in a .c file, in a void function. I have other files with void functions where I initialize const uint8's and there are no warnings in those files. I searched globally, and found no other instance of the variable except where it gets used in the void function (so there aren't any issues with redefining or anything like that). To be clear, these const's are not part of any structure or anything strange, they are just declared in the void function in the .c file.

I have uint8 properly typedef'd, and not #define'd (see comments).

Can someone help me get rid of this warning?

EDIT:
If I paste one of the const uint8's into another .c file directly after a const uint16 that doesn't throw the error, like this:

const uint16 rate_bias_time_constant[NUM_RATE_CHANNELS][RATE_BIAS_STEP_MAX] = 
    {30,120,480,960,  300,120,180,240,  300,120,180,240 }; // comment edited out
const   uint8 SUM_THRESHOLD_MIN = 15;   /* comment edited out   */

I get warning 364 in this file for this const uint8 but not for the const uint16. If I change SUM_THRESHOLD_MIN from a uint8 to a uint16, I still get the warning. For completeness, if I change it from all uppercase to all lower case, I still get the warning.
If I change the line to, e.g.:

const uint8 SUM_THRESHOLD_MIN[2] = {15,2};  /* comment edited out   */

There is no warning.

Best Answer

I got it, and thanks so much for everyone's help!

The declarations that are not throwing warnings are either arrays, or they are declared static const uint8/16's. For some reason, the Hi-Tech C compiler is fine with const int/char's, const uint8/16's that are arrays, but not const uint8/16's unless declared as static const uint8/16.