Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Getting a pointer to a variable by its name as a string

Reply
Thread Tools

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

 
 
archmonkey@hotmail.com
Guest
Posts: n/a
 
      09-15-2005
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

 
Reply With Quote
 
 
 
 
Zara
Guest
Posts: n/a
 
      09-15-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-15-2005
(E-Mail Removed) 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.
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      09-15-2005
(E-Mail 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      09-16-2005
(E-Mail Removed) 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.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      09-16-2005
(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Determining method type given its string name presentation and its corresponding object reference. Apple Python 3 08-01-2005 03:16 AM



Advertisments