5

I am using C language in an embedded system. I have this uint8 array.

static uint8_t data_array[20];

Content of data_array is terminated with '\n'.

I want to check whether the first 3 bytes are "ABC". This is how I do it.

if (data_array[0]=='A' && data_array[1]=='B' && data_array[2]=='C')
{
    printf("pattern found\n");
}

Are there more elegant ways to detect the pattern? My method can be cumbersome if the pattern consists of 10 bytes.

Netwave
  • 40,134
  • 6
  • 50
  • 93
  • 2
    `char` should be used for storing characters. – 2501 Jul 21 '16 at 09:40
  • 1
    Does the code rely on a specific character set, ASCII, for example, or does it need be able to handle others? – alk Jul 21 '16 at 09:45
  • @alk Good point. But as long as characters are used consistently, the results will be valid. I'm pretty sure they must fit into range 0-127. Edit: it must fit into a `char`. So that range isn't even valid. – 2501 Jul 21 '16 at 09:53
  • My concern is more about the implicit conversion from `int` (the `char` literals) to an 8-bit `unsigned`. Imagine an implementation for a platform with `CHAR_BIT == 9` and `'A' == 0b1 0100 0001`. @2501 – alk Jul 21 '16 at 09:59
  • @alk Yeah, see the comment edit above. ( uint8_t cannot exist with CHAR_BIT ==9 , but still, uint8_t may have trap values ) – 2501 Jul 21 '16 at 09:59
  • Ok, an explicity question: Are you after detecting any possible encoding of `char[3]={'A', 'B', 'C'}` or are you "just" after `char[3]={65, 66, 67}`? – alk Jul 21 '16 at 10:03
  • 1
    Funny, how a deceptively simple question like this, requires several standard citations and/or assumptions to make a correct answer. – 2501 Jul 21 '16 at 10:03
  • "*`uint8_t` cannot exist with `CHAR_BIT == 9`* sure? @2501 – alk Jul 21 '16 at 10:05
  • @alk Absolutely. uintN_t types must have no padding. – 2501 Jul 21 '16 at 10:05
  • @2501: Well yes, fair enough. So then I see no more issues with the OP's code. – alk Jul 21 '16 at 10:09

4 Answers4

0

Just use a loop:

static uint8_t data_array[20] = "ABC";
static uint8_t s[4] = "ABC";
static uint8_t length = 3;

uint8_t bool = 1;
for (int i = 0; i < length; i++) {
    if (s[i] != data_array[i]) {
        bool = 0;
        break;
    }
}
if (bool) {
    printf("pattern found\n");
}

Live code here

Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
  • 1
    This `uint8_t s[4] = "ABC";` makes assumption which not necessarily are met on any implementation/platform. – alk Jul 21 '16 at 10:01
0

What about strncmp()?

static uint8_t data_array[20];

if(strncmp(data_array, "ABC", 3) == 0)
{
   printf("pattern found\n");
}
alk
  • 69,737
  • 10
  • 105
  • 255
Batuu
  • 595
  • 1
  • 8
  • 22
-1
char small='a' ,big='A';
int i=0;
char arr[10];

//To check 

for(i; i<10 ; i++)
{


    if(arr[i]==small || arr[i]==big)
    {
          //what you want to do 
    }
    small++;
    big++;



}
tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49
-1

You can utilize macros to generate code similar to your:

#include <stdio.h>


#define TEST_FIRST_1(arr, c0)       ((c0) == *(arr))
#define TEST_FIRST_2(arr, c0, ...)  (TEST_FIRST_1(arr, c0) && TEST_FIRST_1((arr) + 1, __VA_ARGS__))
#define TEST_FIRST_3(arr, c0, ...)  (TEST_FIRST_1(arr, c0) && TEST_FIRST_2((arr) + 1, __VA_ARGS__))
#define TEST_FIRST_4(arr, c0, ...)  (TEST_FIRST_1(arr, c0) && TEST_FIRST_3((arr) + 1, __VA_ARGS__))
#define TEST_FIRST_5(arr, c0, ...)  (TEST_FIRST_1(arr, c0) && TEST_FIRST_4((arr) + 1, __VA_ARGS__))
/* continue, if need */


static unsigned char data[30] = {
    1,2,3,4,5,6,7,8,9,10,11,12,13,
};

int main(int argc, char **argv)
{
    /* true cases */
    printf("%d\n", TEST_FIRST_1(data, 1));
    printf("%d\n", TEST_FIRST_2(data, 1, 2));
    printf("%d\n", TEST_FIRST_3(data, 1, 2, 3));
    printf("%d\n", TEST_FIRST_4(data, 1, 2, 3, 4));
    printf("%d\n", TEST_FIRST_5(data, 1, 2, 3, 4, 5));

    /* false cases */
    printf("%d\n", TEST_FIRST_5(data, 0, 0, 0, 0, 0));
    printf("%d\n", TEST_FIRST_4(data, 0, 0, 0, 0));
    printf("%d\n", TEST_FIRST_3(data, 0, 0, 0));

    return 0;
}

It may appear to be not clean at first look, but anyway doesn't lead to creating of an extra string literals for pattern to compare with.

Sergio
  • 8,099
  • 2
  • 26
  • 52