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 CCS Technical Support

How to Check values of an array??

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



Joined: 26 Apr 2006
Posts: 38
Location: Portugal

View user's profile Send private message

How to Check values of an array??
PostPosted: Thu May 11, 2006 4:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 11, 2006 4:22 pm     Reply with quote

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







PostPosted: Thu May 11, 2006 4:26 pm     Reply with quote

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







PostPosted: Thu May 11, 2006 4:59 pm     Reply with quote

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

View user's profile Send private message

re:
PostPosted: Thu May 11, 2006 5:22 pm     Reply with quote

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
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