consider code below, run on laptop with following output
it first displays the "empty" array. then adds two entries with "keys" "a" and "b". then processes two additional entries with keys "a" and "c", the one with key "a" overwriting the existing entry for "a"
dispMyDataSet:
0:
1:
2:
3:
4:
dispMyDataSet:
0: a AB UQ -129 AB
1: b NB OU -123 JB
2:
3:
4:
dispMyDataSet:
0: a OP QX -151 YZ
1: b NB OU -123 JB
2: c bb oo -100 jj
3:
4:
#include <stdio.h>
#include <string.h>
typedef struct {
char messageFromPC [2];
char sID[3];
char dID[3];
char pRssI[5];
char route[3];
} MyDataSet_t;
#define N_DATA 5
MyDataSet_t myDatatable [N_DATA] = {};
// -----------------------------------------------------------------------------
MyDataSet_t *
searchMyDataSet (
char *s )
{
MyDataSet_t *p = & myDatatable [0];
// search for matching entry
for (unsigned i = 0; i < N_DATA; i++, p++)
if (! strcmp (p->messageFromPC, s))
return p;
// search for empty entry
p = & myDatatable [0];
for (unsigned i = 0; i < N_DATA; i++, p++) {
if (! *(p->messageFromPC))
return p;
}
return 0;
}
// -----------------------------------------------------------------------------
void
readMyDataSet (
char *buf )
{
char tempChars [80];
char *key;
strcpy (tempChars, buf);
strcpy (key, strtok (tempChars, ", "));
MyDataSet_t *p = searchMyDataSet (key);
if (0 == p) {
printf ("%s: no available entry\n", __func__);
return;
}
strcpy (p->messageFromPC, key);
strcpy (p->sID, strtok (NULL, ", "));
strcpy (p->dID, strtok (NULL, ", "));
strcpy (p->pRssI, strtok (NULL, ", "));
strcpy (p->route, strtok (NULL, ", "));
}
// -----------------------------------------------------------------------------
void
dispMyDataSet (void)
{
MyDataSet_t *p = myDatatable;
printf ("%s:\n", __func__);
for (int n = 0; n < N_DATA; n++, p++)
printf (" %d: %s %s %s %s %s\n", n,
p->messageFromPC, p->sID, p->dID, p->pRssI, p->route);
printf ("\n");
}
// -----------------------------------------------------------------------------
int
main ()
{
dispMyDataSet ();
readMyDataSet ("a,AB,UQ,-129,AB");
readMyDataSet ("b,NB,OU,-123,JB");
dispMyDataSet ();
readMyDataSet ("a,OP,QX,-151,YZ");
readMyDataSet ("c,bb,oo,-100,jj");
dispMyDataSet ();
return 0;
}