Variable Name

This is a discussion on Variable Name within the C++ Programming forums, part of the General Programming Boards category; Hello Is it possible to do something like this: Code: int main() { //an array of variable names is made ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile Variable Name

    Hello

    Is it possible to do something like this:

    Code:
    int main()
    {
        //an array of variable names is made here
        //(type?) name_array[2] //two names are prepared
        
        int name_array[0]; //the first variable is declared as an integer and takes on the name of the first element in name_array
        float name_array[1]; //the second variable is declared as a float and takes on the name of the second element in name_array
    
        name_array[0] = 1; //values are 
        name_array[1] = 1.0; //assigned here
    }
    Something along the lines of an array containing prepared variable names distinguished solely by their index.

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    106
    You would have to declare an array of floats(correct me if im wrong)

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    Code:
    struct Data
    {
        int m1;
        float m2;
    };
    
    std::map<std::string, Data> Map;
    Map["my_var_name"].m1 = 0!
    Map["my_var_name"].m2 = 1.0f;
    
    auto it = Map.find("my_var_name");
    if (it != Map.end()) ; // Variable name is in map; otherwise not
    See documentation for std::map.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    26
    The Bad news is what you are asking is impossible. The Good news you do not need it.
    You can't name variables stored in an array. But just the name of the array is sufficient to access these variables. So, you don't need that feature.

    If you really want to name the variables, you can create references to variables stored in an array.

    What Elysia showed is storing variables specified in the logic of the program, which is not the same as the C++ variables.
    Last edited by salquestfl; 08-05-2010 at 07:46 PM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    But you can "map" strings to specific elements in an "array". Not directly, but this is essentially the same. You have a string, and you want to store specific data is identified by this string, For example.
    This is what this is good for. But beware, the lookup time is O(logN), not O(1).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 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