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

STRING FUNCTIONS

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



Joined: 26 Feb 2013
Posts: 30
Location: Algeria

View user's profile Send private message

STRING FUNCTIONS
PostPosted: Tue Feb 26, 2013 7:12 am     Reply with quote

Hi guys, I have a problem in using the structure STRCPY copy. I want to copy a part from a well-defined string so I copied the letters one by one and then I concatenate formed to the desired word, but always shows me nothing.

Below is my code:
I use PIC C compiler v4.130

Code:
#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <string.h>

char s4[20]="abcpomefg";
char s1[20],s2[20],s3[20];


void main()
{

   strcpy(s1, s4[3]); 
   strcpy(s2, s4[4]); 
   strcpy(s3, s4[5]); 
   
   strcat(s1,s2);
   strcat(s1,s3);
   
   printf("RESULT= %s\n",s1);

  while(1);
 
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19346

View user's profile Send private message

PostPosted: Tue Feb 26, 2013 7:32 am     Reply with quote

strcpy, expects an address for both variables. So:

strcpy(s1, &s4[3]);

or

strcpy(s1, s4+3);

You then look to run out of space in the catenation.
s1 is initially 9 characters (10 array entries). You then add 6 characters to this (so 15 entries needed), then five, so need _21_ characters to store the final string. Remember a string is always one character longer than the text it contains.

Best Wishes
vortexe90



Joined: 26 Feb 2013
Posts: 30
Location: Algeria

View user's profile Send private message

PostPosted: Tue Feb 26, 2013 8:01 am     Reply with quote

First of all I thank you "Telmah", but with this method I can just copy the last part of my string
that is to say if I strcpy (s1, s4+1), the result will be all of the string s4, except the first letter.

In my case I just want to copy the letters "p", "o", "m" of my s4 string [20] = "abcpomefg"

in my case I want to have the following result "pom" in s1



thank you in advance
Ttelmah



Joined: 11 Mar 2010
Posts: 19346

View user's profile Send private message

PostPosted: Tue Feb 26, 2013 8:42 am     Reply with quote

Yes.

However to just copy three letters, look at strncpy. Exactly the same as strcpy, except it just moves 'n' characters.

Best Wishes
vortexe90



Joined: 26 Feb 2013
Posts: 30
Location: Algeria

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 2:44 am     Reply with quote

Thank you very much my friend, it was working well
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 5:03 am     Reply with quote

Hi! I have similar problem. I want to make a big string from few smaller.
Here is my code:
Code:

int year=2013;
int month=2;
int day=27;

char yearString[5];
char monthString[3];
char dayString[3];

char space=" ";

char bigArray[11];

void TransformIntToString()
{
    sprintf(yearString,"%lu",year);
    sprintf(monthString,"%lu",month);
    sprintf(dayString,"%lu",day);
}

void FillBigArray()
{
    strcopy(bigArray,yearString);
    strcat(bigArray,space);
    strcat(bigArray,monthString);
    strcat(bigArray,space);
    strcat(bigArray,dayString);
}

There are several problems.
First: every string is added on different places in the bigArray. I think this is because it`s not creared!
Second: every strcat adds string on the old one and deleting it!
How can I fix this??
Thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19346

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 6:21 am     Reply with quote

You are making things about 50* more complex than they need to be:
Code:


sprintf(bigstring,"%4Lu %2u %2u",year,month,day);

You also need year to be declared as int16, or 'long int'. An int cannot hold 2013, and the %Lu format string tells the compiler to treat the number as a 'long', which an int is _not_, which is probably why the first version goes wrong.

Best Wishes
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Thu Feb 28, 2013 12:40 am     Reply with quote

Thanks!
vortexe90



Joined: 26 Feb 2013
Posts: 30
Location: Algeria

View user's profile Send private message

PostPosted: Thu Feb 28, 2013 3:14 pm     Reply with quote

what means that the operator :
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Feb 28, 2013 3:32 pm     Reply with quote

vortexe90 wrote:
what means that the operator :
Sorry, but this is just a very basic C question, not specially related to embedded programming using the CCS compiler.
Search the internet for the phrase "conditional operator".
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