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

"printf" and char, help please

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







"printf" and char, help please
PostPosted: Thu Jun 12, 2008 1:56 am     Reply with quote

hello,
I need help for a verry stupid thing :

I use CCS for PIC and I need to creat a string and send it on rs232
I try it :

Code:
char text [];
text = "test";
printf(text);
printf("%s",text);

but it doesn't work

How can I do it?
How can I add caracter to text? (ex: text=text+"h", text=text+104)

thanks
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Jun 12, 2008 2:09 am     Reply with quote

First you need to specify the space required including the null terminating character for a string, unless you initialise it in the declaration e.g.

char text[] = "test";

or

char text[5];
// len = 5, "text" = 4 chars + null terminating char to indicate the end of the string

Secondly you can only do an assignment to an array at declaration as shown above. The way you need to do it later on is to use a standard string function called strcpy, there are many others, strlen, strcat, strncpy etc.

char text[5];
strcpy(text, "test");

now printf("%s", text); should work.
spilz
Guest







thanks
PostPosted: Thu Jun 12, 2008 2:55 am     Reply with quote

thanks, it works

I have an other question : how can I add caracter?

ex : I receive caracter by caracter and I would like make a string with them
"t"+"e"+"s"+"t"="test"

thanks
treitmey



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

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

PostPosted: Thu Jun 12, 2008 8:06 am     Reply with quote

If you are receiving them into an array, simple put in a '\0' at the end and the array becomes a string. k&R pg38
note: you have to allow space to store the extra character.

Code:
#include <16F877a.h>
#device *=16   //not needed for now.
#use delay(clock=20000000)
#fuses hs,nowdt,nolvp,protect,put,nobrownout
#use rs232(baud=19200,xmit=PIN_B3,INVERT,stream=DEBUG) // STDERR(same as DEBUG)
#use rs232(baud=1200,errors,xmit=PIN_C6,rcv=PIN_C7,parity=e,enable=PIN_C5,bits=8,stream=CIM)
#case
#zero_ram
//======================= Main ==============================
void main(void)
{
  int8 x=0;
  int8 my_array[2]={0x48,0x69};//"Hi";
  int8 my_string[3];

  //===================== STARTUP SCREEN=====================//
  x=sizeof(my_array);
  while(x--)   
  {             
    my_string[x]=my_array[x];//copy the array
  }
  my_string[sizeof(my_string)-1]='\0';//terminating character in last position
  fprintf(DEBUG,"start");
  fprintf(DEBUG,"%s\n\r",my_string);

  while(1)
  {
  }
}



Last edited by treitmey on Thu Jun 12, 2008 8:32 am; edited 3 times in total
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Jun 12, 2008 8:21 am     Reply with quote

I think the function you are looking for is strcat().
_________________
The search for better is endless. Instead simply find very good and get the job done.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Jun 12, 2008 9:50 am     Reply with quote

Basic char array and string info.

A string is stored in a char array.
The char array can be any size as long as it is big enough to store any string you want to put in it + the null terminating character and you have enough ram for it.

char myStr[10];
char myStr2[100];

What makes an array of chars a string rather than just an array of chars is that the last char has a value of '\0', 0x00, null (the null termination char).

char myStr[100];
myStr[0] = 'A';
myStr[1] = 'B';
myStr[2] = 'C';
myStr[3] = '\0';
printf("%s", myStr); // prints "ABC"
myStr[3] = 'D';
myStr[4] = '\0';
printf("%s", myStr); // prints "ABCD"

A simpler way.

char myStr[100];
strcpy(myStr, "ABC");
printf("%s", myStr); // prints "ABC"
strcat(myStr, "D");
printf("%s", myStr); // prints "ABCD"

notice, my array size of 100 is plenty to store these strings. It is a common problem that you do not alloacate enough space. If you do not reserve enough space then when you do:-

char myStr[2];
strcpy(myStr, "abcdefgh");

It will not fail but could cause your PIC to crash.
spilz
Guest







thanks
PostPosted: Fri Jun 13, 2008 6:22 am     Reply with quote

it is exactly what I surch

I'm surprise than a string of 100 char uses a lot of ram whereas it's only 100 octets, and if I use the eeprom, it uses less space (but too slow for my program (rs232 115200bd))

thanks again for your help

spilz
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Jun 13, 2008 7:00 am     Reply with quote

As long as you have enough ram available and you need persistant storage what you should do is copy the data from eeprom or flash to a ram array and when it changes or at certain points, copy it back.

There are issues with this such as the pic being turned off or crashing before you write the data to eeprom or flash.

If you are constantly updating the eeprom/flash due to changes then you may not see much of a speed increase.
You also need to think about wearing the flash/eeprom out due to excessive writes.

The amount of memory used is dependant on a few things.
If the array is defined as a const then extra code is used to access the data which is added after the array.
if you are using Flash (Program memory) which is either 12 or 14 bit wide then you can theoretically save space by squashing the data up. I think the CCS compiler can handle this for you.

I am sure others can comment on this.
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