accessing a variable whose name isnt known until runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drsmooth
    New Member
    • Oct 2007
    • 112

    accessing a variable whose name isnt known until runtime

    is there a way to append a number to a variable name at runtime?

    like this
    i have 100 TImages
    i want to make an array with 1 pointer to each image

    i dont want to say:

    ptrs[0] = timage1;
    ptrs[1] = timage2;

    ect.. to 100

    (actually i found nearly 700 in my program so this would look quite untidy...)

    what i would like to do is run a for loop
    where the variable stored as a pointer is bgased on the number in the for loop

    is there any way to do something like that??

    thanks
    ken
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    I’ve seen this variable name at runtime idea come up quite frequently. It doesn’t make sense, and here’s why. A variable name is something that you come up with at compile time, because unlike a computer, you need to deal with actual names with your variables. It’s convenient to be able to call the objects you create by some name.

    Now, it’s not like the computer deals in names. When it runs a program, it’s not like it says, alright set variable min to 4 and max to 8. No. It’ll allocate memory as appropriate, and refer to those memory locations with addresses. And it will work with those addresses. The concept of variable names is only there for you as a programmer. Therefore, the concept of dynamic names does not exist. If the program is going to create some memory, you are going to have instruct it do so at compile time. Hence, you can give a name to that object that occupies memory at compile time. My wording isn’t so great, but it’s a simple concept if you think about it for a minute or two. Tell us if you don’t quite get it. It’s fundamentally you understand - with absolute clarity why dynamic variable names at runtime does not exist in C++.

    So I’ll turn to what you want to accomplish.

    “i have 100 TImages” What are TImages? How are these 100 stored?

    “i want to make an array with 1 pointer to each image” A pointer in what sense?

    “actually i found nearly 700 in my program so this would look quite untidy.” What do you mean you ‘found’ 700?

    Comment

    • drsmooth
      New Member
      • Oct 2007
      • 112

      #3
      i think i understand what you're saying,

      the solution being i could just know where the labels are stored and they will be sequential in the memory?

      the timages are IDE components in borland c++ builder
      i have 759 of them as they are placed in a big grid(lots of copy and paste to make them);

      but if they are all compiled one after t he other they should all be next to each other in the memory?

      **im editing this after i tried to put some code together with my understanding of what you told me

      i came up with this:

      Code:
      const int GRID_WIDTH = 33, GRID_HEIGHT = 23;
      TImage *gridImages[GRID_WIDTH][GRID_HEIGHT];
      
       TImage *ptr = Image1;
       for(int x =0;x<GRID_WIDTH;x++)
       {
        for(int y =0;y<GRID_HEIGHT;x++)
        {
         gridImages[x][y] = ptr;
         ptr+=sizeof(TImage);
        }
       }
      on that i get an eaccessviolatio n

      thanks
      ken

      Comment

      • drsmooth
        New Member
        • Oct 2007
        • 112

        #4
        i actually solved that problem...for now it seems to be working..thanks alot

        i understand what you said and after grinding away for awhile i came up with this:

        Code:
         TImage **ptr = &Image1;
         for(int y =0;y<GRID_HEIGHT;y++)
          for(int x =0;x<GRID_WIDTH;x++)
          {
            gridImages[x][y] = *ptr;
            ptr++;
            gridImages[x][y]
          }
        i used ** becuase i realized the IDE handles the labels with variable names as *TImage so,
        Image1 = *TImage

        thanks for your help

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          the solution being i could just know where the labels are stored and they will be sequential in the memory?
          They won’t be sequential unless you have an array of those labels.

          i have 759 of them as they are placed in a big grid(lots of copy and paste to make them);
          Wow. This is something that should have been done with code, not with the IDE component builder.

          but if they are all compiled one after t he other they should all be next to each other in the memory?
          If you have three TImage variables, it is not guaranteed they will be sequential in memory. You must have a TImage array for that to happen. It’s only sequential if you allocate a large memory block. Which is what creating an array does.

          I don’t work with Borland, so I can’t verify your code. It looks fishy though...

          Comment

          • drsmooth
            New Member
            • Oct 2007
            • 112

            #6
            ideally i would have made them at runtime, but i am unfourtunatly new to this ide and dont know how to get them to show up on the form...

            im going to have to figure it out soon because it is causing problems with its functionality.. .

            thanks though,
            ken

            Comment

            • Cucumber
              New Member
              • Sep 2007
              • 90

              #7
              What you want to do is not available in C or C++ natively.

              It is possible to create and figure out variables at run time with other (usually scripted) languages, for instance PHP.

              If you want to achieve something similiar in C++, you could use a map of strings and variable values. The strings would hold the "variable name" and the second value in the map would store actual variable value.

              You need to use a string class to shorten the amount of code.

              Example, without error checks, just to get my point through:
              Code:
              #include <map>
              using namespace std;
              ...
              // Declaring the map, maybe as a global? dunno
              map< string,int > myintegervariables;
              ...
              // Creating a variable on the fly
              char s[32];
              printf("Gimme the variable name:\n");
              scanf("%s",s);
              fflush(stdin);
              int var;
              printf("Gimme the variable value:\n");
              scanf("%d",&var);
              fflush(stdin);
              pair < string,int> avar (s,var);
              myintegervariables.insert(avar);
              ...
              // Accesing the variable name
              char s[32];
              printf("Gimme the variable name:\n");
              scanf("%s",s);
              fflush(stdin);
              string str(s);
              map< string,int  >::iterator it;
              it = myintegervariables.find( str );
              if( it!=myintegervariables.end() )
                  printf("Variable &s is %d\n", str.c_str(), (*it).second );
              else
                 printf("No such variable dude\n");
              ...
              Use this only if you really really need to.

              Comment

              Working...