Question:
how to search from a list of variable by its value? then display the name of that variable?
Lokman
2012-07-24 06:43:46 UTC
Type of programming : C or C++
I don't know if I ask the right question or not.
Here is the problem: There is a list of variables. The user will enter the number, then it will display the name of the variable, not the value.
eg:
AA = 1;
BB = 2;
CC = 3;
DD = 4;
the user will enter the number : 3
it will display : the name of the variable is "CC"
Three answers:
?
2012-07-24 11:12:39 UTC
Yes, you can do something like that. There are a number of strategies you can choose from, though. You might allow the user to enter assignment statements and you would then keep track of variable names THEY create (not you), perhaps using a hashtable or linked list or array or other mechanism, and you could certainly look things up in that table. There might be more than one that matches and what you do about it is up to you.



But I'll assume, for a moment, that you want these to be internal variables within the C code itself. If that's the case, you might have your program use the map file generated by the compiler, parse that, and then have the linker also generate it's own detailed listing of memory locations, match all that up, and then take into consideration how the operating system loads and places your program to do this in a general way. If you compile your program for debugging, the compiler cooperates by placing additional information in the object file and the linker cooperates by placing a great deal more information into the executable. But even then, variables may reside on the stack, in heap, as well as in static locations... so you need to learn a great deal with no complete guarantees that you will find access to everything you want. Perhaps a subset is all you can get.



But if you already know the list of variables you want to examine, then it is much easier. Just prepare a table and use it. Here is an example in C:



#include

#include

int AA= 1;

int BB= 2;

int CC= 3;

int DD= 4;

typedef struct {

    const int * const varaddr;

    const char * const varname;

} varlist_t;

const varlist_t list[]= {

    { &AA, "AA" }, { &BB, "BB" }, { &CC, "CC" }, { &DD, "DD" }

};

int getint( const char * const s ) {

    char buf[200];

    printf( "%s: ", s );

    if ( fgets( buf, sizeof(buf), stdin ) == NULL ) return 0;

    return (int) strtol( buf, NULL, 10 );

}

int main( void ) {

    int i, v= getint( "Enter value to search for" );

    for ( i= 0; i < sizeof(list)/sizeof(list[0]); ++i )

        if ( *list[i].varaddr == v )

            printf( "Found the value in variable %s\n", list[i].varname );

    return 0;

}



Something along those lines. If you construct the table values within a function, you could get access to local, automatic variables as well. It works with heap, too. But the problem in general is that you may need ways to add or subtract variables from the list if their lifetimes aren't static. When a pointer becomes invalid, you need to be certain the variable is removed. And when a pointer becomes valid, it needs to be added. Static lifetime variables have the advantage that they exist for the duration of the program, so it's lots simpler to manage the list for those.



Anyway, you need to be a lot more specific about WHY you want to do this before an appropriate answer can be directed squarely to the question.
justme
2012-07-24 07:23:09 UTC
No you cant because when the code is compiled, the variable names are basically converted to a memory location. In other words, the actual variable names (ie: "AA" or "CC") don't exist in compiled code
?
2016-12-13 10:26:38 UTC
remark the section with which you're exhibiting the hyperlink checklist after which try if the you may seek a fraction. i think of you're making specific alterations to the regulations together as exhibiting the related checklist.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...