#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    6
    Rep Power
    0

    newbie variable scope / lifetime question


    im not sure if this has to do with variable scope or lifetime, or if its just bad code, but it says "inf" is an undefined variable


    if(typ == REG_DWORD) { DWORD inf = strtoul(fnd, 0, 0); }
    else if(typ == REG_SZ || typ == REG_EXPAND_SZ) { char *inf = fnd; }
    else { typ = NULL; int inf = 0; }

    cout << inf;

    bascially im trying to specify a dynamic variable type based on what the variable "typ" is. Is there a better way to go about this, or am i just missing something from what i already have?
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Code:
    if(typ == REG_DWORD) 
    { 
       DWORD inf = strtoul(fnd, 0, 0); 
    }
    inf is known from the point at which it is declared to the end of the containing block, which in this case is the closing brace which immediately follows it. After that closing brace it's out of scope.

    Code:
    else if(typ == REG_SZ || typ == REG_EXPAND_SZ) 
    { 
        char *inf = fnd;
    }
    Likewise for this inf which is a different variable in a different block.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    In C++ (your could wouldn't have compiled in C), a variable on exists within the code block that it is declared in. So after you exitted whichever if-else block you had passed through, inf no longer existed.

    Besides, just from a semantical perspective, just exactly what was cout << inf; supposed to mean to the compiler? An int? A char array? A DWORD? How was the compiler supposed to know what kind of code to generate for that statement when it couldn't know what specific data type to expect? The entire idea behind programming is describing unambiguously to the computer what we want it to do.

    I would think either put the cout in each of the if-else code blocks or else put the different possible cout's in a switch statement whose cases are the data types. Or else have each if-else code block create a character string and have cout output that string.
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    So, is there no non-hackish way to dynamically type variables? I guess a union would be the way actually now that i think about it, yes?
    Last edited by infamous41md; September 5th, 2003 at 04:58 PM.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally posted by infamous41md
    So, is there no non-hackish way to dynamically type variables? I guess a union would be the way actually now that i think about it, yes?
    Probably. But then while reviewing my functions for reversing byte-order in numeric variables (for handling a serial port protocol; hton* was not an option), my boss started to ask why I hadn't used unions, then stopped, looked at me, and said, "I guess you're just not a union man." Having grown up working construction with my father, a small-time general contractor, I had to agree. [grin]

    C++ added all those fancy new kinds of type casting that I haven't gotten around to reading about. Would they be of any use?

IMN logo majestic logo threadwatch logo seochat tools logo