![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
compare 2 strings |
Posted: Thu Sep 14, 2006 8:44 am |
|
|
What would be the fastest and shortest way to compare 2 strings?
Code: | const int8 Command_Strings [2][5] = {
"COM1"
"COM2"
}; |
and
Code: | char Temp_Array [4]; |
Now when receiving characters / commando's from another processor, I need to read them.
How would yo do:
Code: | if ( char Temp_Array [4] == "COM1" )
return COM1_COMMAND_READ; |
Now I'm using:
Code: | // Commando gaan vergelijken met de geprogrammeerde commando's
// Schrijf Braille
strcpy (command,BRAILLE_COMMANDO);
if ( !strncmp (temp_array,command,4) )
return SCHRIJF_NAAR_LEESREGEL;
// Na 30s idlen gaat de E3 in suspend
strcpy (command,E3_SUSPEND);
if ( strncmp (temp_array,command,4) == 0 )
return E3_IN_SUSPEND;
// Batterij commando?
strcpy (command,BATTERIJ_COMMANDO);
if ( strncmp (temp_array,command,4) == 0 )
return VBAT_METEN; |
So actually the same code (= waste of rom?) again.
How can this be done with the fewest rom and the fastest way? My gues is that this smells like pointers, however you cannot create pointers to constants I read in the manual.. |
|
![](templates/subSilver/images/spacer.gif) |
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Thu Sep 14, 2006 9:39 am |
|
|
strcmp()
sorry - fingers on the keyboard before the brain was engaged. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Last edited by asmallri on Thu Sep 14, 2006 10:04 am; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu Sep 14, 2006 10:01 am |
|
|
He is already using strncmp.
The problem is that the internal compare functions, do not accept constant strings. Therefore he has the overhead of copying the strings to RAM, and then comparing.
There are a number of possible answers. If you just declare the strings as normal, not constant strings:
Code: |
int8 Command_Strings [2][5] = {
"COM1"
"COM2"
};
|
Then the compiler will copy them from ROM into RAM, _once_ during the initialisation (if you put them outside any function) . Strcmp, or strncmp, can then be directly used. The downside is the amount of RAM used (though small for just these two strings).
The alternative is to remember that there is nothing 'special' about strcmp, except the fact that it is already written for you. You can perfectly well compare the characters, with the character constants yourself, on a character by character basis, without having to copy them to RAM. Even better, for two strings that contain the same characters, why not just look for the single string, and then look for the extra charater. So something like:
Code: |
char COM_STR[4]="COM";
#define COM1 (1)
#define COM2 (2)
#define NO_MATCH (3)
int8 look_for_com(char * string_to_test) {
if (strncmp(string_to_test,COM_STR,3)==0) {
//Here 'string_to_test', contains 'COM'
if (string_to_test[3]=='1') return COM1;
if (string_to_test[3]=='2') return COM2;
}
return NO_MATCH;
}
|
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|