'struct name' does not name a type

I am facing an error in code below:

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



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

it is showing error:

'MyDataSet_t' does not name a type

Please help..

Don't use typedef in C++, use

struct MyDataSet_t {
  char ...
};

If that doesn't solve the issue, try manually adding a function prototype:

MyDataSet_t *searchMyDataSet (char *s );

As a side note, your argument s should probably be const char *.

sry,can you further explain on this? because the one u said earlier does not solve the issue

Add this line:

Below your struct definition and above the function definition.

1 Like

so you are pointing the location I put as comment?

Yes. If that doesn't work either, you'll have to post your entire sketch and the full error message.

It shows no error! but May I know what does it mean? are you saying to declare the 'function' before working inside?, like putting some order in the function? and why you said not to use typedef?

Google "C++ function prototype". In this particular instance, it's an issue with the Arduino IDE prototype generator that reorders the automatically generated prototype before the struct definition. If you manually add a prototype, it won't be generated in the wrong place.

Because typedef is a keyword from the C language. Arduino sketches are written in C++, which is a different language. Using typedef struct in C++ is pointless, and for type aliases you should use the using keyword instead of typedef.

so it is like "declaring a function" before I am using it, am I right? but it is only applied on struct ?

because these are the actions to be done in the function, so I am assuming it to be the way I mentioned earlier? to declare the function first

Yes, although it is not strictly necessary in this case, since you're not using (calling) the function before it is defined.

You can do it for all functions, but in this case you need it because the Arduino IDE makes a mistake when generating the prototype for you.

1 Like

on another topic, do I need to declare a function name for typedef there? starting from there until the end of my searchDataSet ? to be put them in main / void loop function etc?

I'm not sure what you mean. Like I mentioned earlier, you shouldn't use typedef in C++: the struct keyword always introduces a new type, you shouldn't write typedef struct.

You can use typedef struct in C, but Arduino sketches are C++, so unless you need compatibility with other C code (which is not something I'd recommend if you're just starting out), don't worry about it, just forget about the typedef.