|
|
View previous topic :: View next topic |
Author |
Message |
cjusto
Joined: 26 Apr 2006 Posts: 38 Location: Portugal
|
How to Check values of an array?? |
Posted: Thu May 11, 2006 4:01 pm |
|
|
Hi forum!!
can someone help me if it is possible to do what i want, and how to do it.
i have an array, lets say 11 postions.
i need to check in a if condition if the position 3 to 7 has some information.
Code: |
BYTE in_buffer[10] = {0,0,0,0,0,0,0,0,0,0};
(...)
void main ()
{
if(in_buffer [1] == 1) //
if(in_buffer [2] == 48) //0
if(in_buffer [3] == 48) //0
if(in_buffer [4] == 49) //1
(...)
}
|
i can i check those values in the "in_buffer" array??
is there anything like in_buffer[1:4] == {1,48,48,49}; ???
i hope someone can help me with this question. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 11, 2006 4:22 pm |
|
|
You can use the memcmp() function. If all the bytes match, it will
return 0.
Also note that in C, the first index in an array is 0. So if you
want to start comparing at the 2nd index, then you have to
pass its address to memcmp() as shown in the code below.
I say this because in the code that you posted, you're starting
your comparison at the 2nd array index (i.e., the index = 1).
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>
int8 in_buffer[10] = {0,1,48,48,49,0,0,0,0,0};
int8 key[4] = {1,48,48,49};
//===========================
void main()
{
int8 result;
result = memcmp(&in_buffer[1], key, sizeof(key));
printf("result = %x\n\r", result);
while(1);
}
|
|
|
|
Ttelmah Guest
|
|
Posted: Thu May 11, 2006 4:26 pm |
|
|
Yes, you can compare the values as you show.
Probably the easiest 'shortcut', is to remember that an array of bytes, is the same as a string array (except beware that you won't have the required null terminator). Hence you can use the 'strcmp' function to compare the required array with your buffer (or probably preferably, the 'strncmp' function, which only compares 'n' characters, getting rid of possible problems with noth having a terminated array).
Best Wishes |
|
|
KamPutty Guest
|
|
Posted: Thu May 11, 2006 4:59 pm |
|
|
If you control the data, meaning you designed and programmed it..
Can you use bit settings? Granted you are limited to "0" and "1". But with the bit settings, you can easily do "does x=y?"
You could also copy that buffer to a temp buffer (but only the elements you need), end the temp buffer with NULL, then do a string compare...
Create a struct/union with all this data (11 elements, etc), so you can check the specific elements
food for thought...
~Kam (^8* |
|
|
cjusto
Joined: 26 Apr 2006 Posts: 38 Location: Portugal
|
re: |
Posted: Thu May 11, 2006 5:22 pm |
|
|
Hi!
thanks a lot PCM programer. it works.
hey guys, thank for the answers. with PCM programmer's code it's fine.
thanks. see you around |
|
|
|
|
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
|