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

Using Preprocessor macros for register declaration and index

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



Joined: 04 Apr 2011
Posts: 5

View user's profile Send private message

Using Preprocessor macros for register declaration and index
PostPosted: Tue Nov 04, 2025 1:05 pm     Reply with quote

I am using the following preprocessor macro to declare registers and also create an index for each declared register:

Code:

const int8 RegCnt = 0;
#define REG_DEFINE_ARRAY(name,dv,size) \
  int8 name[size]; \
  const int8 Offset_##name = RegCnt; \
  const int8 DefaultValue_##name = dv ; \
  const int8 RCT = RegCnt; \
  #undef RegCnt \
  const int8 RegCnt = (RCT + size); \
  #undef RCT


This macro is called multiple times as shown here:

Code:
//
// Register name             DefaultValue           Size     
// -------------             ------------           ---
REG_DEFINE_ARRAY(AdminPwd    , 0x01                  , 8     )
REG_DEFINE_ARRAY(SiteIDMorse , 0x00                  , 6     )
...many other registers declared here...


This compiled successfully when using V4.140.

When compiling with V5.121, this error appears:

Code:
 
1 Errors,  0 Warnings, Build Failed.,  Time: 1 Seconds
--- Info 300 "<path>/MyPIC.h" Line 101(165,171): More info:   First Declaration of RegCnt
Error[31]   <path>/MyPIC.h 134 : Identifier is already used in this scope


Line 134 points to the first REG_DEFINE_ARRAY call.

Is there a way to use preprocessor macros to declare and count registers that would be compatible with V5.121?
Ttelmah



Joined: 11 Mar 2010
Posts: 19970

View user's profile Send private message

PostPosted: Wed Nov 05, 2025 11:05 am     Reply with quote

The problem is that #undef doesn't do what you think.
It should not have worked. That it did was a fluke....
#undef undefines a macro, not a variable name. Hence the failure.
You don't actually need to undef at all. Just use a different name.
C can be made to generate a unique variable name each time a macro
is called.

There are some bits of this that seem silly/pointless.
const, says to put a variable into the ROM. You are telling the compiler to
put RCT into ROM, and then tell it to forget about it at the end of the macro.
Why?. Just make this a macro definition, and it can then be forgotten with
#undef. It never needs to actually be a variable.
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