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

Datatypes

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



Joined: 09 Oct 2008
Posts: 16

View user's profile Send private message

Datatypes
PostPosted: Fri Dec 05, 2008 5:39 am     Reply with quote

Hi

I'm trying to make a data type ITEM which has a variable type, name and pointer to something else but my code errors with;
'Expression must evaluate to a constant' and 'Expecting a declaration' on the first line of initializing.

How can I get around this problem? I'm not experienced with making data types.

I am using PCH Compiler v4.080

Code:

typedef struct ITEM_STRUCT
{   
   int8 Type;
   char *Text;
   char *Pntr;
} ITEM;

ITEM Group[] = {
   {0, "TEST", 0},
   {3, 0, 0},
};
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 05, 2008 11:47 am     Reply with quote

I changed the structure element to an array and it compiled.
Code:
#include <18F452.H>
#fuses HS,NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP   
#use delay(clock=20000000)                           
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

typedef struct ITEM_STRUCT
{   
   int8 Type;
   char Text[5];
   char *Pntr;
} ITEM;

ITEM Group[2] =
   {
   {0, "TEST", 0},
   {3, "", 0}
};


//=========================
void main()
{

while(1);
}                             
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Sat Dec 06, 2008 12:13 am     Reply with quote

I've done this sort of thing, but I'm pretty sure you need a "const":
Code:
const ITEM Group[] = {
  //...
};

_________________
Andrew
Wipster



Joined: 09 Oct 2008
Posts: 16

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 6:32 am     Reply with quote

Well I did a bit of thinking and with the error 'Expression must evaluate to a constant' I decided to try:

Code:
typedef struct ITEM_STRUCT
{   
   int8 Type;
   const char *Text;
   const char *Pntr;
} ITEM;


But then it says 'Unknown type' on the first line with the const, could this be a compiler bug?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 1:23 pm     Reply with quote

Here are some threads about using const values in structures:
http://www.ccsinfo.com/forum/viewtopic.php?t=32666
http://www.ccsinfo.com/forum/viewtopic.php?t=32604
http://www.ccsinfo.com/forum/viewtopic.php?t=30610
http://www.ccsinfo.com/forum/viewtopic.php?t=28682
http://www.ccsinfo.com/forum/viewtopic.php?t=22070
Wipster



Joined: 09 Oct 2008
Posts: 16

View user's profile Send private message

PostPosted: Wed Dec 10, 2008 7:09 am     Reply with quote

Thanks for linking those threads unfortunately I have already read them, it doesn't look like I can have a pointer to "TEST" because it says it needs to be a constant but you cant have a const pointer because it complains unknown type.
The reason I need pointers is that Pntr should point to another instance of ITEM.

Is there any way around it?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Dec 10, 2008 9:31 am     Reply with quote

I think, the confusion comes from the fact, that C generally allows to use a string constant in assignments in place of a char* pointer. But it's important to understand, that the compiler has to insert a pointer to an implicitely generated string constant.

With CPU's that have different pointer types for data and program memory, as a PIC, it cant' work without copying a rom string constant to data memory before.

The above defined struct ITEM can't hold the string itself, only a pointer, which must be a pointer to data memory (at least in default #device CONST = ROMI mode).

Other pointer options are mentioned in CCS C readme.txt, you can check if they are suitable for your application. A pointer to data memory can only be initialized with the address of a data memory object, or NULL. The below construct compiles without errors.

Code:
char ctext1[] = "TEST";
ITEM Group[] = {
   {0, ctext1, 0},
   {3, 0, 0},
};


Last edited by FvM on Wed Dec 10, 2008 9:36 am; edited 1 time in total
Ttelmah
Guest







PostPosted: Wed Dec 10, 2008 9:33 am     Reply with quote

A whole 'suite' of different solutions, depending on the likely sizes and numbers of the strings involved.
First, store the text message into RAM. Then pointers to these are fine.
Second, don't store a pointer, but store an array index into a larger 'message' array. If (for instance), you make a 20 line constant array, containing the messages, then all that needs to be stored in the structure, is the row numbr containing the required message for the particular item.
Third, if you have a late model compiler, you can have _ROM_ declarations (rather than 'CONST' declarations), that support pointers. You can switch the behaviour of the compiler so const uses this form, but these declarations take more space, and are slower than const declarations, so to my mind it is better to use the separate 'ROM' keyword, and avoid this where it is not needed.

Best Wishes
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