Getting a pointer to a variable by its name as a string

Discussion in 'C Programming' started by archmonkey@hotmail.com, Sep 15, 2005.

  1. Guest

    Hello

    Is there a way to get a pointer to a variable through the variable name
    represented as a string?

    Let me explain it through an example:

    /* Start code example */

    int MyVar = 3;
    int * MyVarPtr;

    MyVarPtr = SomeFunction("MyVar");

    /* End code example */

    That is, MyVarPtr will be the same as &MyVar

    Hope you can make some sense out of this

    Thanks in advance

    Martin Nilsson
     
    , Sep 15, 2005
    #1
    1. Advertising

  2. Zara Guest

    wrote:
    > Hello
    >
    > Is there a way to get a pointer to a variable through the variable name
    > represented as a string?
    >
    > Let me explain it through an example:
    >
    > /* Start code example */
    >
    > int MyVar = 3;
    > int * MyVarPtr;
    >
    > MyVarPtr = SomeFunction("MyVar");
    >
    > /* End code example */
    >
    > That is, MyVarPtr will be the same as &MyVar
    >
    > Hope you can make some sense out of this
    >
    > Thanks in advance
    >
    > Martin Nilsson
    >


    No, in a built-in way.

    You may create an array of structs, each struct having a name and a
    pointer, to a variable. And then writing the search algorithm.

    If you do it, use some kind of macro (#define) to fill every
    name/pointer value, so that no typo may break your work
     
    Zara, Sep 15, 2005
    #2
    1. Advertising

  3. wrote:
    > Hello
    >
    > Is there a way to get a pointer to a variable through the variable name
    > represented as a string?


    No. This question has been asked enough times that it should have been
    easy for you to find it with groups.google.com. If that is too much
    effort, then you should at least have done what is expected before
    posting *any* question to a newsgroup. Not to do this is antisocial.
    Had you so done, you would have found
    <http://www.eskimo.com/~scs/C-faq/q20.6.html>. Admittedly, you might
    have had trouble associating the question answered, "If I have a char *
    variable pointing to the name of a function, how can I call that
    function?" with the one you asked.
     
    Martin Ambuhl, Sep 15, 2005
    #3
  4. SM Ryan Guest

    wrote:
    # Hello
    #
    # Is there a way to get a pointer to a variable through the variable name
    # represented as a string?

    It's not guarenteed to be possible.

    # /* Start code example */
    #
    # int MyVar = 3;
    # int * MyVarPtr;
    #
    # MyVarPtr = SomeFunction("MyVar");

    In general by the time a C program is run, the association between
    the string making the variable name and the address of the variable
    is lost. If the compiled code contains debugger tables or you have
    included your own symbol table, it may be possible, but these are
    not guarenteed to be available everytime.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I'm not even supposed to be here today.
     
    SM Ryan, Sep 15, 2005
    #4
  5. Simon Biber Guest

    wrote:
    > Hello
    >
    > Is there a way to get a pointer to a variable through the variable name
    > represented as a string?
    >
    > Let me explain it through an example:
    >
    > /* Start code example */
    >
    > int MyVar = 3;
    > int * MyVarPtr;
    >
    > MyVarPtr = SomeFunction("MyVar");
    >
    > /* End code example */
    >
    > That is, MyVarPtr will be the same as &MyVar


    You can do something like that:

    #include "symbol_table.h"

    #include <stdio.h>

    int main(void)
    {
    declare(int, MyVar);
    declare(int*, MyVarPtr);

    MyVarPtr = get_symbol("MyVar");

    printf("&MyVar = %p\n", (void *) &MyVar);
    printf("MyVarPtr = %p\n", (void *) MyVarPtr);

    return 0;
    }

    Where symbol_table.h contains:

    #ifndef H_SYMBOL_TABLE
    #define H_SYMBOL_TABLE

    #include <stddef.h>

    struct symbol {
    const char *name;
    void *pointer;
    } *symbol_table;

    size_t symbol_table_size;
    size_t symbol_table_capacity;

    void add_symbol(const char *name, void *pointer);
    void *get_symbol(const char *name);

    #define declare(type,name) type name; add_symbol(#name, &name);

    #endif

    And symbol_table.c contains:

    #include "symbol_table.h"

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    void add_symbol(const char *name, void *pointer)
    {
    if(!symbol_table_capacity)
    {
    symbol_table_capacity = 16;
    symbol_table = malloc(symbol_table_capacity * sizeof *symbol_table);
    }
    if(symbol_table_size == symbol_table_capacity)
    {
    symbol_table_capacity = symbol_table_capacity * 2;
    symbol_table = realloc(symbol_table,
    symbol_table_capacity * sizeof *symbol_table);
    }
    if(!symbol_table)
    {
    fprintf(stderr, "Error allocating memory for symbol table\n");
    exit(EXIT_FAILURE);
    }
    symbol_table[symbol_table_size].name = name;
    symbol_table[symbol_table_size].pointer = pointer;
    symbol_table_size++;
    }

    void *get_symbol(const char *name)
    {
    size_t i;
    for(i = 0; i < symbol_table_size; i++)
    {
    if(!strcmp(symbol_table.name, name))
    return symbol_table.pointer;
    }
    return NULL;
    }

    --
    Simon.
     
    Simon Biber, Sep 16, 2005
    #5
  6. wrote:
    >
    > Is there a way to get a pointer to a variable through the variable
    > name represented as a string?
    >
    > Let me explain it through an example:
    >
    > int MyVar = 3;
    > int * MyVarPtr;
    >
    > MyVarPtr = SomeFunction("MyVar");


    You've had a few answers to this, but you haven't stated _why_ you
    feel you need to do this in the first place.

    --
    Peter
     
    Peter Nilsson, Sep 16, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apple
    Replies:
    3
    Views:
    324
    Apple
    Aug 1, 2005
  2. Bobby Chamness
    Replies:
    2
    Views:
    2,440
    Joe Smith
    Apr 22, 2007
  3. thunk
    Replies:
    1
    Views:
    359
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    536
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    659
    thunk
    Apr 3, 2010
Loading...

Share This Page