Hey hey,
I've been looking through the Arduino forum and other website for an answer to my problem but i didn't find anything
So my question is:
If I have a structure like:
struct example {
char brand[8];
int dataSize;
int temp[BTT];
char valMode[3][BTT];
{...}
};
How can I have an access to a setting according to the user input (instead of doing several if loops)
For example if I get "Temp" from the user (or a char variable that I can modify), is there a way to use it to call example.Temp instead of:
if (strcmp(data, "temp") == 0)
Serial.println (example.Temp[0]);
Because I have a lot of variables in my sructure and i don't want to write a loop for each of them.
First, the struct definition you provided and the subsequent access to it, is not correct. However, I'll assume that just because you weren't really posting the code. Second, C is case sensitive, so example.Temp[0] isn't going to work. Third, you could devise some form of hash code that would index into the structure. However, hash tables often have huge holes in them and you don't have a lot of memory to waste. Finally, you wouldn't have to write an if statement block for each string. You'd write a function that receives the string to locate and pass back the index of where's it's found, or, perhaps, -1 if it is not found.
I can't figure what you are trying to do - you are just describing a "how" problem.
If you want a system in which a user can arbitarily send an item of data that needs to be compared with one of several different data items already in the Arduino maybe you need a different approach.
How, for example, would the user identify which data he is sending?
Is the data being sent from another program or by a user typing at a keyboard ?
Have you considered using a union so that the struct can be addressed in different ways for different purposes ?
oh ok I Think I don't describe my question correctly
And no I didn't write my entire code because it would be too long and I have a lot of functions Calling each other
Thank you Arank but I don't want to replace the data in my structure by the data given by the user, i want to access a part of my structure according to the char that i give
so (this is definitely wrong, but this is to explain how i want it to work)
char example[] = "temperature";
then I would like to be able something like:
name_of_my_structure.example = 101;