-2

I'm using 'const int' and 'char const * const', to inform compiler that my value will never change. I need to succeed minimum program and data memory.

   typedef struct{
         char const * const   Name[2];
         const int  MaxValue;
         const int  MinValue;
         const int  Type;
         const int  EppromLocation;
         const int  NextID;
         const int  PreviousID;

      union{ 
      unsigned int DefaultValue;
      unsigned char bytes[2];
      }SetValues;

   } Parameters;

  extern volatile  Parameters MenuRegister[50];

I'm getting errors:

a.c:119: error: (364) attempt to modify object qualified const
a.c:123: warning: (358) illegal conversion of pointer to integer 

the error is showing in my source file

 MenuRegister[0].Name[0]="A";
 MenuRegister[0].Type=1;
 MenuRegister[0].SetValues.DefaultValue=1;
 MenuRegister[0].EppromLocation=1;
 MenuRegister[0].Visible=true;
 MenuRegister[0].NextID=1;
 MenuRegister[0].PreviousID=1;
 MenuRegister[0].MaxValue=1;
drs
  • 133
  • 1
  • 11
  • 2
    What exactly is your question? What have you tried so far? – dbush Jun 20 '18 at 18:32
  • 1
    So is the problem that you aren't initializing, and since they are const, you cannot set them after you've created them? – Christian Gibbons Jun 20 '18 at 18:32
  • 1
    I'm getting error a.c:119: error: (364) attempt to modify object qualified const a.c:123: warning: (358) illegal conversion of pointer to integer – drs Jun 20 '18 at 18:34
  • 1
    Please edit the error message into your question. Please add the code that error is pointing at to your question. Please mark the exact line that's referred to as `a.c:123` –  Jun 20 '18 at 18:39
  • 2
    `volatile` is in some ways the antithesis of 'constancy'. It says "the value may change in ways the compiler cannot expect". The `const` says "the value cannot be changed via this name. You've not shown the lines where you're getting the error messages — nor should you; line 119 is too big to be an MCVE ([MCVE]). You should be able to get the line number down to about 20, and you can then show it. Your `MenuRegister` must be initialized fully when it is created; it cannot be modified meaningfully (you can modify `DefaultValue` and `bytes`, in the `SetValues` union member, but that's all). – Jonathan Leffler Jun 20 '18 at 18:39
  • *I need to succeed minimum program and data memory* is, unfortunately, not clear at all. Please try stating your goal in a different way. –  Jun 20 '18 at 18:40
  • @drs edit your error into the question. – Christian Gibbons Jun 20 '18 at 18:42
  • I update question. I need to have static values and non static values in same typedef – drs Jun 20 '18 at 18:52

1 Answers1

3

The problem is that you cannot set these const-qualified values after instanciation. You will need to initialize the const-qualified struct members upon creation.

Perhaps like so (I simplified to an array of 2 because I wasn't gonna do that for 50 of 'em in an example):

MenuRegisters[2] = {
   {.Name = {"A","B"},
    .Type=1,
    .SetValues.DefaultValue=1,
    .EppromLocation=1,
    .Visible=true,
    .NextID=1,
    .PreviousID=1},
   {.Name = {"C","D"},
    .Type=2,
    .SetValues.DefaultValue=2,
    .EppromLocation=2,
    .Visible=true,
    .NextID=2,
    .PreviousID=2}}

Also want to note that Visible is not in the definition of Parameters you gave us.

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29