1
static const unsigned char pkt1[89] = {
0x00, 0x00, 0x5e, 0x00, 0x01, 0x12, 0x00, 0x18, /* ..^..... */
0x82, 0x5e, 0x5a, 0xf6, 0x08, 0x00, 0x45, 0xb8, /* .^Z...E. */
0x00, 0x4b, 0x9d, 0x0d, 0x00, 0x00, 0x3f, 0x11, /* .K....?. */                                          
};

/* Frame (89 bytes) */
static const unsigned char pkt2[89] = {
0x00, 0x00, 0x5e, 0x00, 0x01, 0x12, 0x00, 0x18, /* ..^..... */
0x82, 0x5e, 0x5a, 0xf6, 0x08, 0x00, 0x45, 0xb8, /* .^Z...E. */
0x00, 0x4b, 0x9d, 0x25, 0x00, 0x00, 0x3f, 0x11, /* .K.%..?. */
}

....
...

till pkt[100]; 


int main()
{

char buff[10]="pkt";
for(i=0;i<100;i++)
{
itoa(1,buff+3,10);
printf("%s",buff);
}

}

From the above code I am able to see the concatenated name as pkt1 / pkt2... till pkt100. But I dont know how can I access the elements of pkt1 now. Any suggestions how to do it are welcome. Can I do this runtime ?

1 Answer 1

1

Not without implementing it yourself, no. C does not retain variable names at run-time, so if you want to use the name to map to an array you're going to have to implement it yourself:

const unsigned char * get_pkt(const char *name)
{
  if(strcmp(name, "pkt1") == 0)
    return pkt1;
  if(strcmp(name, "pkt2") == 0)
    return pkt2;
  return NULL;
}

UPDATE: If the number of things you need to match is large, code like the above becomes both tedious (and error-prone!) to write, and can affect performance. In those cases, the proper solution is to put the objects into some key-value data structure, such as a hash table. Note that C does not come with a built-in hash table data type, so you're on your own. There are many implementations available, though: glib's GHashTable is one.

If you want to stay within the C standard library, I would go with putting the data in an array, sort it (using qsort()) and then binary-search it using bsearch(). For ~1000 keys, that should do no more than ~10 string comparisons.

Even with these tricks, you won't get away from a need to initialize the data structure, of course. With the arrays, you might be able to do the array initialization in the same motion as you define your packets:

static struct {
  const char *name;
  const unsigned char packet[89];
} packets[] = {
  { "pkt1", { 0x00, 0x00, 0x5e, 0x00, 0x01,  /* ... more */ } },
};

This merely re-organizes the source text you already have, and adds some punctuation. If you take care to give these in the proper order, you don't need to sort at run-time, either!

Sign up to request clarification or add additional context in comments.

3 Comments

if i have 1000 packets.. then I have to write 1000 if else statements.. which I dont think is the right way for programming
@Learner True, I failed to notice that part of your question. I updated my answer.
Can u give me an example here ? @unwind: so in the hash table I need to write thousand lines to map it one on one. Is it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.