Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access a Variable from an other variable ??

Status
Not open for further replies.

modnm

Programmer
Mar 12, 2003
3
0
0
GR
Hi can anyone help me??

String VS1="GOOD BOY",VDD1="GOOD GIRL"….(do not know how many)

String VO , VR ;

If VO="VS1" then I want VR to return “GOOD BOY”

If VO="VDD1" then I want VR to return “GOOD GIRL” and so on..
 
function is in string.h

strcmp();

e.g strcmp(VO,VS1)

returns 0 if equal and non 0 if not.

String Myfunc(String VO, String VS1, String VDD1){
if(strcmp(VO,VS1) ==0){
return(“GOOD BOY”)
}
else if(strcmp(VO,VDD1) ==0){
return("Good Girl");
}
}

Jim ----------------------------------------
Friends are generally of the same sex, for when men and women agree,it is only in the conclusions; their reasons are always different.
 
Hm, I'm not happy about this, but I'm not an expert. By rights it ought not to be possible.

Reason: I think you want to get the contents of a string variable at run-time by refering to it by its name (which you have in another variable). But the name is just what you use to tell the compiler what you are referring to, at compile time. The name you use, at least in theory, need not appear anywhere in the compiled product - after all, the processor doesn't refer to variables by name, and the compiled product is for the processor, to use at run time.
Therefore to ensure that the contents of a variable could be got at run-time by name reference, all compilers would have to provide a list of variable names in the compiled exe product, which would encourage very inefficient exe files. It sort-of bypasses the point of using a get-between-the-bytes compiled language like C++, and tends towards the province of an interpreted language.

But maybe I've misunderstood what you want to do.

You can of course write your own variable-tracking system where you retain names of "variables" and associated values, and write your own function that returns the value when it's passed the name. I think that is what doherty is doing.

Please tell me someone if I'm writing total rubbish...
 
lionelhill , that was exactly that i wanted to do... thanks a lot. It seems that i am not going to solve my prob...
 
I might be utterly wrong, though, so watch this space! It's amazing how often someone comes back and either (a) tells me how to do the impossible, or (b) says where to find some library that helps to provide what's needed to solve the problem.
 
What exactly is the problem you need to solve?

Can you use an associative array instead? Can you emulate the effect by using pointers/references?
 
Are you wanting to load vr with a copy of or a pointer too. it's all in the pointers.

doherty has the idea.

tomcruz.net
 
No, I think what he/she is trying to do is to make one variable refer to another by name. He has a string variable called VS1, and he has another string holding the text "VS1", and he wants to pass this second string variable to a function that will then identify the first variable by its name ("VS1") and return the contents of the first string variable.

Do let me know, modnm, if I've misunderstood you. I'm guessing this on the basis that others have asked similar questions.

I think this would be asking a bit much of a compiler because normally, at compile time, it will translate all the names of variables into appropriate addressing, and the names themselves then lose all significance, and need not even feature in the final exe file (though for all I know they might do. I've never gone looking. They probably do in files compiled as suitable for debugging). So for instance a global variable called "bloggs" is probably just an offset of, say, 268, in the data segment, so far as the compiled file is concerned. It no longer has the name "bloggs" associated with it at run time.
It is not in the nature of a compiled language to refer to variables by name in strings, though it is very much in the nature of an interpreted script language.

But I'm sure there are plenty of pointer-based ways to achieve the right effect in different ways. After all, the compiler itself is just a program that can quite happily identify variables by their names!
 
lionelhill Hi
No You did not misunderstood my .
That is exactly what I want to do .
But my question remains. How does the debugger refers to a variable by its name, and if the debugger can to it, why can’t we??.
 
In order to allow the debugger to refer to variables by name, the compiler has to insert special debug info into the object files. Having that info is the exception rather than the rule.
 
I still think you will use an array and or possibly a linked list if you want to add to the list at run time.

I can see a lookup array sort of thing in your future.
with a dollop of strcmp.

struct data
{
char name [256];
char buffer [256];

struct data *nextptr;
};

//these are aliases for the data structure.
typedef struct data DATA;
typedef DATA *DATAPTR;

/////////////////////////

DATAPTR AddNode (void)
{
DATAPTR newptr = new data;

//Initializes the structure.
newptr->name = "VS1";
newptr->buffer = "Good Boy";

newptr->nextptr = NULL;

//Return the pointer to the new node created.
return newptr;
}

initialize the node in various ways from user input or
however you are getting the variable name.

if (!strcmp (vo, node->name))
put node->buffer wherever you want

yes, maybe, no, kinda sortof.

hope it helps.

tomcruz.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top