Checking for the existence of a variable in C

I've looked for an answer to this, but I can't find a solution.

I have some code that I'm adding a function to. Basically, I'm adding the ability to change some default values, by reading in a file line by line to re-define the default values that the code earlier defined. So when my function reads in "A = 1.0" I want it first to check if A is defined, and if it isn't, return a warning, but not crash or stop.

(More detail: this code is about 20000 lines long, and I can't re-write to make #define statements for every variable. Nor do I wish to print out the hundreds of variables for the user to pick the one he/she wants to change. He/she should have an idea of the name of the variable "wave_period_meters" and if they get it wrong or misspell it, I want them to be able to try again, rather than have the code crash.)

I have no idea what you want to do. If you make sure your variables are initialized you don't need to check if they are defined, you know they are. That may or may not have helped you depending on what your question is exactly. Stab in the dark.

The program (of course) initializes all of the variables it uses. I'm trying to give the user more control over what those values can be set to. So without changing hundreds of lines of code (where the variables are all initialized and set to default values), I'd like to open a file, read the lines (eg. wave_period_seconds = 11.6) and then set that variable "wave_period_seconds" to 11.6 (where the default value was 10.0). But if the user accidently entered "wave_period = 11.6" I want the code not to crash but to warn him that he entered an incorrect variable name.

EDIT:
By which I mean not every variable in the program, of course, but are the "parameters" (the ones you want to be able to reset from a file) globally accessible? If they are locals, then it's hard to see how you can easily graft something onto the program without making changes in each function where the variables are defined. If the parameters are global, you should be able to do something like I've shown above.

Last edited by oogabooga; 09-25-2012 at 02:28 PM.

The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Yes. But 90% percent of the time the default values are used, so the code defines those values (lots of them) throughout the first 1000 or so lines of code. (this code has many revisions and authors). So I check to see if the "config" file exists, if it does, I open it and read in the contents.

If the code knows whether or not a variable has been initialized, why can't we check for existence?

EDIT:
By which I mean not every variable in the program, of course, but are the "parameters" (the ones you want to be able to reset from a file) globally accessible? If they are locals, then it's hard to see how you can easily graft something onto the program without making changes in each function where the variables are defined. If the parameters are global, you should be able to do something like I've shown above.

Yes, all of the values that might be changed are global. I like your idea, and I will have to think about a way to use it in the laziest...errr I mean ... most efficient way possible. :-)

Yes, all of the values that might be changed are global. I like your idea, and I will have to think about a way to use it in the laziest...errr I mean ... most efficient way possible. :-)

If you're dealing with a single code file, you could define the structure at the very bottom (so it can "see" all the variables), or at least after the last parameter is defined. Then define the functions that use it.

Are they all the same type?

The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Yes. But 90% percent of the time the default values are used, so the code defines those values (lots of them) throughout the first 1000 or so lines of code. (this code has many revisions and authors). So I check to see if the "config" file exists, if it does, I open it and read in the contents.

If the code knows whether or not a variable has been initialized, why can't we check for existence?

You need to explicitly check ( a large switch/case statement comes to mind ) . I would create a simple parser that checks for a: "id = value eol" syntax. You check that the syntax is correct, lookup the id and assign the value (and check it's range if that is necessary).

Edit: If the id does not exist, the value is out of range, or the syntax is wrong, print an error message.