Is there a way to know if a variable exists and know its type(int,char,str,...)


My project is a wireless project between two arduinos and I want to be able to change the value of variables on one of the arduinos by sending something like var1=10 from the other. Thats ok as long as var1 exists on the destination arduino, but i would like to first check if it really exists. Is there a way to check if var1 exists before I atempt to change its value. It would also be nice to know what type of variable var1 is(int,char,str,...)

Can anyone help please?



Variable names do not exist in the compiled code. You cannot send an arduino an arbitrary “let var1 = 10” and expect it to do anything. You will need to write a parser that understands your commands.

Thanks KeithRb for your quick reply.

Of course I wrote a parser that interprets my commands and then sets my values. This works fine but from the sender arduino I am able to send var2=10 while maybe var2 does not exist on the recieving arduino. So its possible to send a setting for a variable that does not exist on the reciever arduino. Since I have a lot of variables i wanted to try not make the parser filter the long list of existing variables, but just check if they exist, and if so, set their new value.

Thanks again.

Since I have a lot of variables i wanted to try not make the parser filter the long list of existing variables, but just check if they exist, and if so, set their new value.

It is up to you to parse the string, and find the name and value. Then, YOU need to map the name to an address. You can ONLY store data at addresses. You are responsible for determining if the name means anything.

As I said, variable names are long gone before the program starts running on the arduino. You could create an array of structs or two arrays if you want.

int *varlist = { &var1, &var2, &var3};

char *varnamelist = { "var1", "var2", "var3"};

and search varnamelist to get the index to the pointer you want.

(varnamelist should go into flash to save space)

Thanks PaulS, but i don´t really understand what your saying.

What I do now is when I receive var1=10 is, with a function which I called extract() i extract the var1 value that is received and then assign the received value to my var1 variable. I got this working like this:

var1 = extract("var1").toInt();

But i could also send a var2 value to the receiver on which the var2 variable does not exist so i have to discard it. So on the receiver i would have to make a long list containing all the variables that can be changed and check if the variable that is received is in the list.

It would be much easier if i could check if the variable exists in some way like if if there were a function like

if exist(var2) >>> return true or false


if type(var2) >>> return int, string, char..

Thanks for your help.

Thanks keithRB, I think thats the only way i can go. Maybe its possible to make a function to find out if a variable exists or not and know its type but that escapes from my knowledge. Maybe some arduino Guru can :slight_smile:

Thanks for your help anyway.

What part of "variable names are long gone" is hard to understand? The arduino run-time code only includes addresses, it has no knowledge of any variable names or types you might have written in the source code. You are free to create a table to associate the two, and you can even use the stringizing operator if you want, but that is all done at compile time, not runtime.

Thanks again KeithRB. Now I understand. So my only way to go is compare if the variable received is in a list, and if so assign the value.

Thanks for your help all !!!