CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Expression must evaluate to a constant - Conditional Exp

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
MotoDan



Joined: 30 Dec 2011
Posts: 55

View user's profile Send private message

Expression must evaluate to a constant - Conditional Exp
PostPosted: Mon Jun 08, 2020 9:31 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 08, 2020 9:40 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 08, 2020 9:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 08, 2020 10:03 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 08, 2020 10:24 am     Reply with quote

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: 19538

View user's profile Send private message

PostPosted: Mon Jun 08, 2020 10:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 08, 2020 10:58 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 08, 2020 11:01 am     Reply with quote

Just received an email from CCS. They do not support what I am attempting to do.

Thanks for all the replies.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group