473,324 Members | 2,193 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,324 software developers and data experts.

Dynamically assigning a variable

Hello,

I'm want to set up a function that, based on the string and an int argument that is given to it, will set an an int variable to the given value. The int variable is already declared, so the only thing I need to do is assign a value. But I have no idea what would the syntax for that be!

Here's a pseudo-code example:

int var1, var2, var3;

void setVariable (char *variableName, int variableValue)
{

// code to set the variable, which is passed via variableName, to variableValue

}

So for example, if I call the function with the following - setVariable ("var1", 2);, I'd want to set the variable var1 to the value of 2.

But could someone point me to how might I call the proper variable and assign the value?

Thank you.
Jul 8 '07 #1
3 1651
weaknessforcats
9,208 Expert Mod 8TB
So for example, if I call the function with the following - setVariable ("var1", 2);, I'd want to set the variable var1 to the value of 2.
You have no access to variable names a run time. The compiler has converted your code to machine language. Just 1's and 0's.
Jul 8 '07 #2
You have no access to variable names a run time. The compiler has converted your code to machine language. Just 1's and 0's.
Hm, right.

I was hoping to have a file that would contain all of the variable states, one that I could read when the program is loaded to resume it's previous state. Is there any other way to do this, besides having a lot of else if's for all of the variables?
Jul 8 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Saving a program state is more involved than just assigning values to variables. You have to also save the processor registers, the stack, the heap, etc. The OS programmers call this a context switch and it it done all the time inisde the OS.

I think what you are after is a checkpoint. Here you write selected data to disc. Maybe you get clever:

var1= 10
var2 = 25
var3 = 35

and on the restore you read the file and scan for the =. Then you would need an if and a compare to "var1" and if you found that you would assign 10 to var1.

5000 variables is 5000 if's.

On the other hand, you could be even more clever and write out:

10
25
35

and on the restore your would:
Expand|Select|Wrap|Line Numbers
  1. file >> var1 >> var2 >> var3;
  2.  
No if's.

However, you would need to know the order in which things were written so they could be read back in the same order. If you can't do this, then you are back to if the first aapproach.

I've seen it done both ways.
Jul 8 '07 #4

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

Similar topics

7
by: Ron Garret | last post by:
I need to dynamically generate new types at run time. I can do this in two ways. I can use the "type" constructor, or I can generate a "class" statement as a string and feed that to the exec...
10
by: TheKeith | last post by:
I don't know much about javascript, so take it easy on me. Is there a way to dynamically change a CSS layers dimensions on the fly. Here is what I'm doing. I have a bunch of thumbnails that when...
4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
16
by: sirsean | last post by:
Hi all. I'm trying to dynamically build menus and objects after my page loads. Data is stored in an XML file and is parsed at runtime into Javascript objects. At the moment, I'm working on creating...
11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
7
by: cjl | last post by:
Hey all: I've searched the newsgroup, and googled, but I'm stuck. I want to be able to 'dynamically' add a .js file to a web page after the page has loaded, based on user interaction. For...
3
by: yogi | last post by:
Hi guys, I'm trying to write a program that will read in a series of files and create a 3D array from the files read in for converting 2D images to 3D objects. The values read in will be...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.