By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,318 Members | 2,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,318 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ar********@hotmail.com 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
Nov 15 '05 #2

P: n/a
ar********@hotmail.com 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.
Nov 15 '05 #3

P: n/a
ar********@hotmail.com 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[i].name, name))
return symbol_table[i].pointer;
}
return NULL;
}

--
Simon.
Nov 15 '05 #4

P: n/a
ar********@hotmail.com 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

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.