-1

If I convert const int to int inside a void it works.
But when I create an extern const int it doesn't.

void ReadFromEpprom()
{
  int Start = 343;
  const int End=Start; //This works
}

Example 2

Header File

extern const int End;

Source file

const int End;

void ReadFromEpprom()
{
  int Start = 343;
  End=Start; //This doesn't work
}

In second situation I get error:

(364) attempt to modify object qualified const

How can I solve this?
Should I make it with another way?

anatolyg
  • 26,506
  • 9
  • 60
  • 134
Dim
  • 41
  • 8
  • 3
    Why are you trying to modify a constant, which is by definition immutable? http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html – G_V Feb 05 '18 at 11:33
  • Cause i need to read epprom value inside a variable and also this variable to be used in a array. Array accepts ony constant and Read value from epprom only int so i must convert one of them – Dim Feb 05 '18 at 11:34
  • 2
    If you want to modify `End` it can not be constant. And you can pass non-constant variables to functions declared as taking `const` arguments. If that's your problem. – Some programmer dude Feb 05 '18 at 11:36
  • 2
    So keep the variable mutable until it doesn't need to be? At insertion, assign it to a constant. – G_V Feb 05 '18 at 11:36
  • This looks like an [XY Problem](http://xyproblem.info/). Tell us more about your _actual_ prblem. – Jabberwocky Feb 05 '18 at 11:38
  • The `const int` is actually working exactly as it's supposed to. I wouldn't rush to blame your implementation. – StoryTeller - Unslander Monica Feb 05 '18 at 11:38
  • I cant make global variable const int? – Dim Feb 05 '18 at 11:39
  • You can make a global variable const, but you can't change it. That's what `const` means. – Art Feb 05 '18 at 11:40
  • 1
    Initialization is not assignment. These are different terms and the programmer must be aware of the difference. (Particularly if you end up using higher-level languages than C.) – Lundin Feb 05 '18 at 11:41
  • How can i pass an int value into an array? Array works only with constant – Dim Feb 05 '18 at 11:42
  • Do you mean that you want to use `End` as the size of an array? That's not a problem since C supports [variable-lenght arrays](https://en.wikipedia.org/wiki/Variable-length_array). – Some programmer dude Feb 05 '18 at 11:44
  • 1
    And please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, **Complete**, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Feb 05 '18 at 11:44
  • edit question and add ">" to get yellow box for your error. – ZF007 Feb 05 '18 at 11:53

4 Answers4

0

When you declare a constant variable this means the variable will not change.So it makes sense that constant variables must be initialized immediately.You are doing this in your first example which is correct.In your second example you have a constant variable and then you are trying to modify it's value.This is incorrect since the variable is already constant.

Martin Chekurov
  • 733
  • 4
  • 15
0

The extern here is a red herring.

If you use const int End then you need to initialise End at the point of this declaration. That's because it's const.

So const int End = Start; works fine, but const int End; is not syntactically viable.

In C you can't arrange things so you have a const at global scope and a function that sets the value at run-time. But what you could do is embed the value in a function (as as static), and call that function to initialise and subsequently retrieve the value.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Initialiazation versus assignment:

A const object can be initialized (given a value at the declaration), but not assigned (given a value later).


This is initialization, and "works". It is initialization the local variable End that exist inside ReadFromEpprom().

void ReadFromEpprom()
{
  ...
  const int End=Start; //This works

End=Start; attempts to assigned End that exist at file scope, outside of ReadFromEpprom(). const objects cannot be assigned.

const int End;

void ReadFromEpprom()
{
  ... 
  End=Start; //This doesn't work
}

How can I solve this?

Let external code to read localEnd via a function ReadEnd(), yet allow local code to write localEnd.

static int localEnd;

int ReadEnd() {
  return localEnd;
}

void ReadFromEpprom() {
  int Start = 343;
  localEnd = Start;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

I agree with all the other answers.

It seems that you want to initialize a const value with a value that will be determined at run time, which is impossible.

What you can do is two workarounds.

  1. Remove const. Add a comment near its definition that the variable is set only once in the very beginning.
  2. Use a function instead of a const int variable; implement the "run code only once" idea inside this function.

    Header file:

    int CalcEnd();
    

    Source file:

    int CalcEnd()
    {
        static int init_done = 0;
        static int result;
        if (!init_done)
        {
            result = 343; // or any complex calculation you need to do
            init_done = 1;
        }
        return result;
    }
    

    Note how the function uses static variables and logic to do initialization only once.

If you use the function idea, and don't want to remember typing the parentheses in the function call (CalcEnd()), you can define a macro:

#define End MyCalcEnd()

This will make it appear as if End were const int variable, when actually it's a function call. This usage of macros is controversial, use it only if you are sure it will not lead to confusion later.

anatolyg
  • 26,506
  • 9
  • 60
  • 134