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

A

archmonkey

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
 
Z

Zara

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
 
M

Martin Ambuhl

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.
 
S

SM Ryan

(e-mail address removed) 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.
 
S

Simon Biber

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;
}
 
P

Peter Nilsson

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top