Like Tree1Likes
  • 1 Post By oogabooga

Checking for the existence of a variable in C

This is a discussion on Checking for the existence of a variable in C within the C Programming forums, part of the General Programming Boards category; I've looked for an answer to this, but I can't find a solution. I have some code that I'm adding ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Annandale, VA
    Posts
    4

    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 am new to this, so I hope this makes sense. Thanks! Jim

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There's no information about variable names in a compiled program (other than debugging information, which may lead to some interesting possibilities but I don't know).

    Assuming the simplest situation where are the variables you wish to set are global and are all the same type (say double), you could do something like:
    Code:
    struct {
        char   *name;
        double *data;
    } params[] = {
        {"whatever",              &whatever},
        {"wave_period_meters",    &wave_period_meters{,
        {"something_else",        &something_else}
    };
    I'm assuming it's clear how this structure would be employed. And it could be extended for multiple types.
    christop likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Sep 2012
    Location
    Annandale, VA
    Posts
    4
    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.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Are all the variables global?

    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

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So the file will be like a .config or .rc file basically?

  7. #7
    Registered User
    Join Date
    Sep 2012
    Location
    Annandale, VA
    Posts
    4
    Quote Originally Posted by Subsonics View Post
    So the file will be like a .config or .rc file basically?
    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?

  8. #8
    Registered User
    Join Date
    Sep 2012
    Location
    Annandale, VA
    Posts
    4
    Quote Originally Posted by oogabooga View Post
    Are all the variables global?

    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. :-)

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by James Murray View Post
    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

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by James Murray View Post
    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.
    Last edited by Subsonics; 09-25-2012 at 02:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking a variable
    By rambright in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2010, 08:02 AM
  2. Checking if variable is unitialised
    By 0rion in forum C Programming
    Replies: 5
    Last Post: 09-01-2004, 01:07 PM
  3. checking variable for no value
    By DarkViper in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2004, 10:37 PM
  4. Checking for file existence
    By Sue Paterniti in forum C Programming
    Replies: 3
    Last Post: 05-06-2002, 02:59 AM
  5. DA's existence...
    By ober in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-01-2002, 08:41 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21