|
|
View previous topic :: View next topic |
Author |
Message |
pictastic
Joined: 26 Mar 2008 Posts: 5 Location: Woodbridge,UK
|
Problem with strings |
Posted: Sat Jul 04, 2009 3:48 pm |
|
|
I'm trying to port some code from another complier to CCS and I'm struggling to get anywhere. Any ideas what is going wrong?
Code segment below which doesn't compile. I'm using CCS version 4.084.
Code: |
/* Context definitions */
static char Anything[] = "";
static char Nothing[] = " ";
/* Phoneme definitions */
static char Pause[] = " ";
static char Silent[] = "";
#define LEFT_PART 0
#define MATCH_PART 1
#define RIGHT_PART 2
#define OUT_PART 3
typedef char *Rule[4]; /* Rule is an array of 4 character pointers */
/*0 = Punctuation */
/*
** LEFT_PART MATCH_PART RIGHT_PART OUT_PART
*/
static Rule punct_rules[] =
{
{Anything, " ", Anything, Pause },
{Anything, "-", Anything, Silent },
{".", "'S", Anything, "z" },
{"#:.E", "'S", Anything, "z" },
{"#", "'S", Anything, "z" },
{Anything, "'", Anything, Silent },
{Anything, ",", Anything, Pause },
{Anything, ".", Anything, Pause },
{Anything, "?", Anything, Pause },
{Anything, "!", Anything, Pause },
{Anything, 0, Anything, Silent },
}; |
_________________ PICs do more than washing machines... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 04, 2009 5:31 pm |
|
|
1. Always post your PIC.
2. Preferably, put that code into a test program. Just a minimal program
would have the #include for the PIC, #fuses, #use delay, and a main().
Put that code either above main() or in it (whichever is applicable).
3. Compile the test program and post the error messages. |
|
|
pictastic
Joined: 26 Mar 2008 Posts: 5 Location: Woodbridge,UK
|
Problem with strings (full code) |
Posted: Sun Jul 05, 2009 6:40 am |
|
|
Full listing of the code below. The .h file is first followed by the main code.
Compilation gives bad expression syntax at the commented line followed by numerous "Expecting a declaration" and "Expecting a (" errors. The main code does nothing at all. It the is declaration of the types and arrays that give the problems. The code is for the 18LF14k50 chip.
Code: |
#include <18LF14K50.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV19
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPB //No Boot Block code protection
#FUSES NOWRTB //Boot block not write protected
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOHFOFST
#FUSES WRT0
#FUSES WRT1
#FUSES USBDIV2
#FUSES PCLKEN
#FUSES BBSIZ2K //2K words Boot Block size
#FUSES PLLEN
#FUSES CPUDIV4 //System Clock by 4
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
// End of the t2p.h file
// Start of the main code
#include "C:\Program Files\PICC\Projects\t2p.h"
#include <string.h>
void main()
{
setup_adc_ports(ALL_ANALOG|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC);// This device COMP currently not supported by the PICWizard
setup_oscillator(OSC_8MHZ|OSC_TIMER1);
// TODO: USER CODE!!
}
/* Context definitions */
static char Anything[] = ""; /* No context requirement */
static char Nothing[] = " "; /* Context is beginning or end of word */
/* Phoneme definitions */
static char Pause[] = " "; /* Short silence */
static char Silent[] = ""; /* No phonemes */
#define LEFT_PART 0
#define MATCH_PART 1
#define RIGHT_PART 2
#define OUT_PART 3
typedef char *Rule[4]; /* Rule is an array of 4 character pointers */
/*0 = Punctuation */
/*
** LEFT_PART MATCH_PART RIGHT_PART OUT_PART
*/
const Rule punct_rules[] =
{
{Anything, " ", Anything, Pause }, //Bad expression syntax here
{Anything, "-", Anything, Silent },
{".", "'S", Anything, "z" },
{"#:.E", "'S", Anything, "z" },
{"#", "'S", Anything, "z" },
{Anything, "'", Anything, Silent },
{Anything, ",", Anything, Pause },
{Anything, ".", Anything, Pause },
{Anything, "?", Anything, Pause },
{Anything, "!", Anything, Pause },
{Anything, 0, Anything, Silent },
};
|
_________________ PICs do more than washing machines... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jul 05, 2009 2:30 pm |
|
|
To solve this problem, I stripped it down and made a test program.
I was able to make it work by moving the storage allocations for
the strings so they are outside of the array. Then the initialization
only consists of pointers to the strings. That got rid of the compiler
error messages.
Here's the output of the program below:
Quote: |
A AP
A-AS
.'SAz
#:.E'SAz
#'SAz
A'AS
A,AP
A.AP
A?AP
A!AP
AAAS |
Here's the test program. In some cases where you had empty strings
or spaces, I put in character instead, so I could see the output.
Code: | #include <18F452.h>
#fuses XT,NOWDT,PUT,NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,ERRORS)
static char Anything[] = "A";
static char Nothing[] = "N";
static char Pause[] = "P";
static char Silent[] = "S";
char dash[] = {"-"};
char space[] = {" "};
char period[] = {"."};
char comma[] = {","};
char question_mark[] = {"?"};
char exclamation_point[] = {"!"};
char pound_sign[] = {"#"};
char apostrophe[] = {"'"};
char apostrophe_S[] = {"'S"};
char z[] = {"z"};
char special[] = {"#:.E"};
typedef char *Rule[4];
Rule punct_rules[11] =
{
{Anything, space, Anything, Pause },
{Anything, dash, Anything, Silent },
{period, apostrophe_S, Anything, z },
{special, apostrophe_S, Anything, z },
{pound_sign, apostrophe_S, Anything, z },
{Anything, apostrophe, Anything, Silent },
{Anything, comma, Anything, Pause },
{Anything, period, Anything, Pause },
{Anything, question_mark, Anything, Pause },
{Anything, exclamation_point, Anything, Pause },
{Anything, Anything, Anything, Silent},
};
//=========================================================
void main()
{
int8 row, col;
for(row=0; row<11; row++)
{
for(col=0; col<4; col++)
{
printf(punct_rules[row][col]);
}
printf("\r");
}
while(1);
} |
|
|
|
Ttelmah Guest
|
|
Posted: Sun Jul 05, 2009 3:00 pm |
|
|
I'd 'suspect', that the compiler automatically treats strings inside a declaration as 'constant', till the moment they are actually allocated RAM, by putting them into an array. Works if you declare them as character arrays first, but since they haven't yet been put into RAM, the compiler won't accept trying to generate a pointer to them.....
Best Wishes |
|
|
pictastic
Joined: 26 Mar 2008 Posts: 5 Location: Woodbridge,UK
|
Problem with strings |
Posted: Sun Jul 05, 2009 3:22 pm |
|
|
Cool
Many thanks for the help guys!! _________________ PICs do more than washing machines... |
|
|
|
|
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
|