473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

accessing a variable whose name isnt known until runtime

112 100+
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
Nov 10 '07 #1
6 2691
oler1s
671 Expert 512MB
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?
Nov 10 '07 #2
drsmooth
112 100+
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:

Expand|Select|Wrap|Line Numbers
  1. const int GRID_WIDTH = 33, GRID_HEIGHT = 23;
  2. TImage *gridImages[GRID_WIDTH][GRID_HEIGHT];
  3.  
  4.  TImage *ptr = Image1;
  5.  for(int x =0;x<GRID_WIDTH;x++)
  6.  {
  7.   for(int y =0;y<GRID_HEIGHT;x++)
  8.   {
  9.    gridImages[x][y] = ptr;
  10.    ptr+=sizeof(TImage);
  11.   }
  12.  }
on that i get an eaccessviolation

thanks
ken
Nov 10 '07 #3
drsmooth
112 100+
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:

Expand|Select|Wrap|Line Numbers
  1.  TImage **ptr = &Image1;
  2.  for(int y =0;y<GRID_HEIGHT;y++)
  3.   for(int x =0;x<GRID_WIDTH;x++)
  4.   {
  5.     gridImages[x][y] = *ptr;
  6.     ptr++;
  7.     gridImages[x][y]
  8.   }
i used ** becuase i realized the IDE handles the labels with variable names as *TImage so,
Image1 = *TImage

thanks for your help
Nov 10 '07 #4
oler1s
671 Expert 512MB
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...
Nov 10 '07 #5
drsmooth
112 100+
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
Nov 11 '07 #6
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <map>
  3. using namespace std;
  4. ...
  5. // Declaring the map, maybe as a global? dunno
  6. map< string,int > myintegervariables;
  7. ...
  8. // Creating a variable on the fly
  9. char s[32];
  10. printf("Gimme the variable name:\n");
  11. scanf("%s",s);
  12. fflush(stdin);
  13. int var;
  14. printf("Gimme the variable value:\n");
  15. scanf("%d",&var);
  16. fflush(stdin);
  17. pair < string,int> avar (s,var);
  18. myintegervariables.insert(avar);
  19. ...
  20. // Accesing the variable name
  21. char s[32];
  22. printf("Gimme the variable name:\n");
  23. scanf("%s",s);
  24. fflush(stdin);
  25. string str(s);
  26. map< string,int  >::iterator it;
  27. it = myintegervariables.find( str );
  28. if( it!=myintegervariables.end() )
  29.     printf("Variable &s is %d\n", str.c_str(), (*it).second );
  30. else
  31.    printf("No such variable dude\n");
  32. ...
  33.  
Use this only if you really really need to.
Nov 11 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
1
by: Jeff Smith | last post by:
Can I load custom web user controls dynamically and access the properties and methods without having to explicitly define custom control types (example 2 below). I have custom web control named...
9
by: JohnR | last post by:
I have the name of a control in a string variable and I want to change one of the controls properties. Right now I recursively scan all the controls on the form until I get one whose name matches...
2
by: Joey | last post by:
I am querying a DataSet with LINQ. I am running into a problem when trying to construct my query because in the "from" clause I do not know the table name (range variable) until runtime. Possible...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.