Typedef struct questions

I am planning to store data I obtained from LoRa RFM 95 shield in table form.
Here is how I process my received data.

if (rf95.available())
    // Should be a message for us now   
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);

    if (rf95.recv(buf, &len))
      Serial.print("Got: ");
      strcpy (tempChars,buf);
      char * strtokIndx;
      strtokIndx = strtok (tempChars,",");
      strcpy (messageFromPC,strtokIndx);

      strtokIndx = strtok (NULL,",");

      strtokIndx = strtok (NULL,",");

      strtokIndx = strtok (NULL,",");

      strtokIndx = strtok("NULL",",");

      strtokIndx = strtok("NULL",",");

      Serial.println("Receive failed");

Someone provided me with this but I need further explanation..

typedef struct myDataSet_t {
  char messageFromPC [2];
  char sID[3];
  char dID[3];
  char pRssI[5];
  char route[3];
} myDataSet_t;

myDataSet_t myDatatable[5];

I am planning to arrange the data as example below:

message|| sID || dID || pRssI||route
a || AB || UQ || -129 || AB
b ||NB || OU|| -123 || JA

in which consists of char n int types and I want to overwrite the values whenever I could.. The question is how do I "inform" that there will be not only one message receive? and how do I assign them in sequence accordingly? for loop does not work?

Don't post snippets (Snippets R Us!)

I guess thats all the required code for my problem

my question now is..
how do I assign all my datas in the array sequentially, and the maximum set of datas will be five only, so the array[5] will be enuf

the first code parses a cString that holds apparently various fields (other cStrings) and copy them into buffers (hopefully with the right size, we don't know the types of those variables because you posted a snippet).

the structure seems similar to the buffers you need (possibly)

I'm not sure what the question is...

it's a structure, a user defined data type, in this case, consisting of 5 char arrays which seem awkard.

consider it's use in

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

MyDataSet_t myDatatable [] =  {
    { "1", "s0", "d0", "rssi", "r0" }, 
    { "2", "s1", "d1", "rssi", "r1" }, 
    { "3", "s2", "d2", "rssi", "r2" }, 
    { "4", "s3", "d3", "rssi", "r3" }, 
    { "5", "s4", "d4", "rssi", "r4" }

#define N_DATA (sizeof (myDatatable)/sizeof (MyDataSet_t))

// -----------------------------------------------------------------------------
readMyDataSet (
    int   idx, 
    char *buf )
    char         tempChars [80];
    char        *strtokIndx;
    MyDataSet_t *p = & myDatatable [idx] ;

    strcpy (tempChars, buf);

    strcpy (p->messageFromPC,   strtok (tempChars, ", "));
    strcpy (p->sID,             strtok (NULL, ", "));
    strcpy (p->dID,             strtok (NULL, ", "));
    strcpy (p->pRssI,           strtok (NULL, ", ")); 
    strcpy (p->route,           strtok (NULL, ", "));

// -----------------------------------------------------------------------------

dispMyDataSet (void)
    MyDataSet_t *p = myDatatable;

    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");

main ()
    dispMyDataSet ();

    readMyDataSet (3, "9, x9, d9, xssi, r9, ");
    readMyDataSet (1, "Y, xY, dY, xssi, rY, ");
    dispMyDataSet ();

    return 0;

yet you don't seem to be working with strings. perhaps the fields should be "ints" (or uint16_t) instead of char arrays

The sketch seems to be using a comma separator. That would mean:


What are the maximum number of characters in the fields message, sID, dID, and route?

Typedef-ing a struct is rather pointless, struct equals class in all but the default visibility.

Because the data I received are set to be char so it is quite extra to change to other datatypes

each 5 will do

sry but do you mind to further explain on this?

What do you mean here. It appears that you have a structure which can store the data from a maximum of 5 read operations of the Lora device. Under which circumstances would you start overwriting data in that structure? Is one of the items of data a key or are you interested in building a collection of the β€œbest” values or what ?
At the end, what do you want to do with the data you have collected? Simply print it out on the serial console?

For example, if I receive data for 'b' again next time and the performance is better than previous one.I mean, in terms of value

Typedef-ing a thing that already is a type is pointless.

You are using a named struct and try to give it that same name by typedef,
I'm too lazy to check whether that is even allowed.

struct myDataSet_t {
  char messageFromPC [2];
  char sID[3];
  char dID[3];
  char pRssI[5];
  char route[3];
} myDatatable[5];

BTW, what is the "my" in the name for?
What is wrong with just DataSet, or DataSet_t, if you want to follow _t naming for types?

I guess it is just matter of name..

I see. So it looks like you want to maintain a "table" of the best characteristics of specific senders.
You've given the example 'b'. From the names you have used, I guess that there are a number of PCs sending performance data and you have to maintain a "table" of data samples.
The structure isn't easily addressable for replacing data but I guess that you have been given that and have to work with it.
Are there 5 or less possible senders, such that each sender can be assigned to a specific element in that structure ?

there are only 5 fixed data but might receive data from same node for more than 1 time.. so I was thinking to compare, for example, receiving 2 data from same node, and their pRssI value are being compared to choose better one and I will only store the better one in my table

so you have to:

  1. parse the data from the receive operation
  2. extract the identifier of the sending PC example 'b'
  3. attempt to find any previous data for 'b' in the array of structs.
    3a. If there is no previous data, add a new entry for 'b' together with all its data.
    3b. If there is previous data for 'b', then you have to see if the new data for 'b'
    ( the pRssI value) is "better" than the previous data. If so, replace it.
  4. When you have collected enough samples of data, you want to print the collected data

Is that it ?
If so, where are you stuck ?

Ya, that's basically wat I want but I stuck from step3...

what are the data? are the data ASCII characters?

If you are basing it on your own code from the OP, then you have a variable called messageFromPC
which is the identifier of the sender (I think).
You have to scan that array of structs looking for it maybe something like this :

bool found = false
for ( uint8_t i = 0 ; i < 5 ; i++ ) {
   if (  strcmp( myDataTable[ i ].messageFromPC , messageFromPC ) == 0 ) {
      // we have a match so compare the values of pRssI and leave the best one in myDataTable[ i ].pRssI 
      found = true ;
if ( found == false ) { 
    // not found, so create new entry at next free element in myDataTable[]
    // or report overflow.