View previous topic :: View next topic |
Author |
Message |
MotoDan
Joined: 30 Dec 2011 Posts: 55
|
Expression must evaluate to a constant - Conditional Exp |
Posted: Mon Jun 08, 2020 9:31 am |
|
|
Hello all,
Trying to fill a const array using a conditional expression and am getting an error 27 "Expression must evaluate to a constant". Have tried several things, but no joy.
The code below is a very simple test case and will not be used exactly as written.
I'm using the latest PCM v5.093. Have also tried older versions with same results.
TIA,
MotoDan
Code: |
#include "16F18323.h"
void main()
{
int x, y;
y = (x==5) ? 6 : 7; // works
int const array1[][2] = {
{1,2},
{3,4},
{5,(x==5) ? 6 : 7} // error 27 here
};
}
|
|
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Mon Jun 08, 2020 9:40 am |
|
|
A constant array is an array of constants. x is a variable and is not a constant. y is a variable and may be assigned with another variable. |
|
|
MotoDan
Joined: 30 Dec 2011 Posts: 55
|
|
Posted: Mon Jun 08, 2020 9:52 am |
|
|
In this example, the intent is to fill one element of the array with a constant (either 6 or 7) based on the conditional expression of whether x=5 or not.
This type of conditional fill works with other C compilers. Just not sure why it won't work with CCS. |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Mon Jun 08, 2020 10:03 am |
|
|
The values in the const array are determined at compile time and stored in ROM. The value of variable x is not known at compile time. |
|
|
MotoDan
Joined: 30 Dec 2011 Posts: 55
|
|
Posted: Mon Jun 08, 2020 10:24 am |
|
|
To your point, I changed the code to fill a variable array rather than a constant array. The results are the same.
Code: |
#include "16F18323.h"
void main()
{
int x;
x=5;
int array1[][2] = {
{1,2},
{3,4},
{5,(x==5) ? 6 : 7} // error 27 here
};
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Mon Jun 08, 2020 10:39 am |
|
|
The point is the the ?: operation is part of runtime code. Not something
that is evaluated at the compile time that can not therefore be used to
fill a variable at compile time. |
|
|
MotoDan
Joined: 30 Dec 2011 Posts: 55
|
|
Posted: Mon Jun 08, 2020 10:58 am |
|
|
I guess it's just the way that CCS compilers treat the ?: operation. I've verified my test code works with two other non-CCS 'C' compilers as well as the Arduino compiler. |
|
|
MotoDan
Joined: 30 Dec 2011 Posts: 55
|
|
Posted: Mon Jun 08, 2020 11:01 am |
|
|
Just received an email from CCS. They do not support what I am attempting to do.
Thanks for all the replies. |
|
|
|