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 support@ccsinfo.com

Define a string

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



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

Define a string
PostPosted: Sun Aug 01, 2004 11:43 am     Reply with quote

I'm trying to define 4 different strings options.
Then send to RS232 one of the option as a result of parameter.
I want to make a switch..case syntax and
printf string1 in case of 1, string 2 in case of 2 ...

Can someone help me with the correct definitions.

something like this (this one of course does not work)
...
#define OPTION1 0123text1
#define OPTION2 text2XYZ
#define OPTION3 axcdsw
#define OPTION4 stringnum4
....
switch (result)
{
case (1): printf (OPTION1);
case (2): printf (OPTION2);
case (3): printf (OPTION3);
case (4): printf (OPTION4);
}
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Sun Aug 01, 2004 3:18 pm     Reply with quote

It could be as simple as this.

switch (result)
{
case (1): printf ("0123text1");
case (2): printf ("text2XYZ");
case (3): printf ("axcdsw");
case (4): printf ("stringnum4");
}
Guest








PostPosted: Mon Aug 02, 2004 6:31 am     Reply with quote

yes, but if I want to change the string I need to look into the program.
It is much more elegant to change it just in the definitions.
The strings may appeare few times in the program.
Any more suggestions, please?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Aug 02, 2004 6:53 am     Reply with quote

Code:

#define OPTION1 "0123text1"
#define OPTION2 "text2XYZ"
#define OPTION3 "axcdsw"
#define OPTION4 "stringnum4"
T800
Guest







Re: Define a string
PostPosted: Mon Aug 02, 2004 7:24 am     Reply with quote

maybe something like this ?!

const char OPT1[10]="foo";
const char OPT2[10]="bar";

[...]

printf("%S",OPT1);


rgds

T800
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Aug 02, 2004 7:32 am     Reply with quote

Seems like a big waste of RAM unless CCS added support pointers to ROM strings.
Guest








PostPosted: Mon Aug 02, 2004 8:57 am     Reply with quote

Mark wrote:
Seems like a big waste of RAM unless CCS added support pointers to ROM strings.

Worth saying, that for the example given, where there are only constant strings, the 'better' solution, is:
#define OPTION1 "0123text1 "
#define OPTION2 "text2XYZ "
#define OPTION3 "axcdsw "
#define OPTION4 "stringnum4 "
....
switch (result)
{
case (1): putc(OPTION1);
case (2): putc(OPTION2);
case (3): putc(OPTION3);
case (4): putc(OPTION4);
}

This takes advantage of the CCS 'shortcut', where if a routine that accepts a single integer (like putc), is called with a constant string, the routine will be called repeatedly, with each character in turn from the constant string. This is smaller, and more efficient on RAM useage, than using the printf version, or converting the ROM strings to RAM.

Best Wishes
Ttelmah
Guest







PostPosted: Mon Aug 02, 2004 8:58 am     Reply with quote

Anonymous wrote:
Mark wrote:
Seems like a big waste of RAM unless CCS added support pointers to ROM strings.

Worth saying, that for the example given, where there are only constant strings, the 'better' solution, is:
#define OPTION1 "0123text1 "
#define OPTION2 "text2XYZ "
#define OPTION3 "axcdsw "
#define OPTION4 "stringnum4 "
....
switch (result)
{
case (1): putc(OPTION1);
case (2): putc(OPTION2);
case (3): putc(OPTION3);
case (4): putc(OPTION4);

Remember also you need a 'break' after eack case, unless you _want_ it to print all four strings for the first option.

Best Wishes
}

This takes advantage of the CCS 'shortcut', where if a routine that accepts a single integer (like putc), is called with a constant string, the routine will be called repeatedly, with each character in turn from the constant string. This is smaller, and more efficient on RAM useage, than using the printf version, or converting the ROM strings to RAM.

Best Wishes
gunking88



Joined: 18 Jun 2004
Posts: 11
Location: WV

View user's profile Send private message Send e-mail

hi, Ttelmah, could you check my post at your convenience?thx
PostPosted: Mon Aug 02, 2004 9:21 am     Reply with quote

at http://www.ccsinfo.com/forum/viewtopic.php?t=20085

thanks a lot,

have a good day

gun
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Aug 02, 2004 10:48 am     Reply with quote

What does that have to do with strings?
edi



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

PostPosted: Wed Aug 04, 2004 2:15 pm     Reply with quote

Ttelmah,
Thanks for your help, but your sample gives an error during the compilation:
"A numeric expression must appear here"

the error is for the line : " case (1): putc(OPTION1); "
(at the beginning I put the line : #define OPTION1 "0123text1 " )

Any suggestion please?

Edi
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Aug 04, 2004 2:21 pm     Reply with quote

This compiles just fine:
Code:

#include <18F452.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=57600, xmit=PIN_C6, rcv=PIN_C7) 

#define OPTION1 "0123text1"
#define OPTION2 "text2XYZ"
#define OPTION3 "axcdsw"
#define OPTION4 "stringnum4"


void main()
{
  int result;

  switch (result)
  {
    case (1): printf (OPTION1);
    case (2): printf (OPTION2);
    case (3): printf (OPTION3);
    case (4): printf (OPTION4);
}
  while(TRUE)
  {
  }
}
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

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

PostPosted: Thu Aug 05, 2004 7:47 am     Reply with quote

for my 2 cents worth,... this is how I code something like that...
I think you could also use a switch.


Code:

const char error_txt[16][16] =
{
  "USART overflow",  // -0-
  "TXBuffer ovrflw", // -1-
  "RXBuffer ovrflw", // -2-
  "Bad Checksum",    // -3-
  "LCD busy",        // -4-
  "5",  // -5-
  "6",  // -6-
  "7",
  "8",
  "9",
  "10",
  "11",
  "12",
  "13",
  "14",
  "NO ERRORS"
};

//somewhere else in code
bit_set(ERRORS,1);//set error flag bit 1 for tx overflow

unsigned int16 ERRORS;



void ChkErr(void) //print out any errors that occured
{
  int8 chk=0;
  if (ERRORS==0){return;} //if no errors jump right back out
  for(chk=0;chk<16;chk++)
  {
    if (bit_test(ERRORS,chk))
    {
      bit_clear(ERRORS,chk);
      fprintf(STDERR,"ERROR(%U): %s.\r\n",chk,error_txt[chk]);
      tx_buffer(NACK,sizeof(NACK));
      rx_indx_i=rx_indx_o=0;
    }
  }
}
Ttelmah
Guest







PostPosted: Thu Aug 05, 2004 8:28 am     Reply with quote

edi wrote:
Ttelmah,
Thanks for your help, but your sample gives an error during the compilation:
"A numeric expression must appear here"

the error is for the line : " case (1): putc(OPTION1); "
(at the beginning I put the line : #define OPTION1 "0123text1 " )

Any suggestion please?

Edi

Brilliant! (screams gently).
CCS's own 'shortcut', refuses to work with their own putc function...
This works:
Code:

#define OPTION1 "TestString1"
#define OPTION2 "TestString2"
#define OPTION3 "AnotherTest"
#define OPTION4  "FinalTest"

#inline
void myputc(int val) {
   putc(val);
}

Then in main:
Code:

   switch (sval) {
   case 0:
      myputc(OPTION1);
      break;
   case 1:
      myputc(OPTION2);
      break;
   case 3:
      myputc(OPTION3);
      break;
   case 4:
      myputc(OPTION4);
      break;
   }


Best Wishes
edi



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

PostPosted: Thu Aug 05, 2004 3:18 pm     Reply with quote

Thanks a lot to all.
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