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

struct with a constant member

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



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

struct with a constant member
PostPosted: Sat Nov 22, 2014 1:26 am     Reply with quote

How do I declare a constant int in a struct.
I am trying to do the following, but its causing compilation error.
Code:

typedef struct{
   const uint16_t baud;
   char delimiter[5];
} _DEVICE_SETTINGS;


I want to be able to pass the baudrate to setup_uart function that only accepts constants.

PIC24
Compiler v5.025
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Nov 22, 2014 1:04 pm     Reply with quote

CCS won't let you combine flash and ram in the same structure.
If you want to use 'const' in the normal C way, where a variable
is stored in RAM but is read-only, then you can add this line
after your PIC #include line:
Code:
CONST=READ_ONLY

Then it will compile.
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Sat Nov 22, 2014 1:11 pm     Reply with quote

Key to understand, is that setup_uart, is _not_ a function. It is a preprocessor directive, handled at compile time.
If you want multiple selectable baud rates, you have to do it like:
Code:

#define B9600 1
#define B19200 2
int8 speed=B9600;

   switch (speed)
   {
   case B9600:
      setup_uart(9600);
      break;
   case B19200:
      setup_uart(19200);
      break;
   }


This then generates both sets of code to setup the UART for the two rates.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: struct with a constant member
PostPosted: Tue Nov 25, 2014 6:04 am     Reply with quote

haxan7 wrote:
How do I declare a constant int in a struct.
I am trying to do the following, but its causing compilation error.
Code:

typedef struct{
   const uint16_t baud;
   char delimiter[5];
} _DEVICE_SETTINGS;



Ttelmah has told you that this is not the way to control baudrates.
However, in C in general, but not necessarily in CCS C, const members of structures are unusual, but can be done. The important point to remember is that such consts must be initialised when the structure is instantiated, in other words when an actual instance of one is declared:

Code:

typedef struct _DEVICE_SETTINGS {
   const uint16_t baud;
   char delimiter[5];
} _DEVICE_SETTINGS;

_DEVICE_SETTINGS My_Settings = { 9600, "abcd" };


After a structure is created, const members cannot be assigned: they are const after all!

In CCS C consts and normal variables are stored in totally separate memory spaces. This restricts what can be done with consts. The compiler cannot ordinarily create a pointer to a const. Consts often cannot be used a parameter to expecting a non-const. String literals, e.g. "This is a string", are different from variable arrays of char. Routines expecting one will not normally handle the other.

To make consts readable from RAM requires some special code, such as the #device CONST=READ_ONLY, which places all consts in RAM rather than ROM - that might also make it possible to have a pointer to consts. For strings there is #device PASS_STRINGS=IN_RAM that forces string literals (i.e. string constants) to be copied into RAM before being passed as parameters to routines. Both use more RAM than the default methods so are not much use if you want to pass large const arrays, such as parsing tables, message lists and graphical display font maps, which are larger than the available RAM.
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