to get macro name from macro value

S

sounak

How could we get a macro name from a macro value
such that
in a header file
#define a 100
#define b 200
now the source file will be such that
the user gives 100
then the value is outputted as a
the user gives 200
then the value is outputted as b


is it possible
thanks in advance
regards
sounak
 
M

Marc Boyer

Le 22-11-2005 said:
How could we get a macro name from a macro value
such that
in a header file
#define a 100
#define b 200
now the source file will be such that
the user gives 100
then the value is outputted as a
the user gives 200
then the value is outputted as b

is it possible

What did you really need ? What would be the
result with:
#define a 100
#define b 200
#define c ((b)-(a))
#define d (a<<1)
#define e 100

Marc Boyer
 
R

Richard Bos

Marc Boyer said:
What did you really need ? What would be the
result with:
#define a 100
#define b 200
#define c ((b)-(a))
#define d (a<<1)
#define e 100

....and the user enters 350?

Richard
 
S

sounak

i just wanted to get the macro name of the specified macro value
just the opposite what macro does
#define a 100
we specify 100 we get output as a
sounak
 
M

Marc Boyer

Le 22-11-2005 said:
i just wanted to get the macro name of the specified macro value
just the opposite what macro does
#define a 100
we specify 100 we get output as a

Please, answer my questions ? What if two macros
have the same value ?

Marc Boyer
 
S

sounak

i need to get the macro name from the value
like suppose
#define a 100

if now i give input to a function as 100 then a should be
returned
just the opposite of the defination we see in macro

do we have to access the sysbol table
if yes then how could we do that
and is there any other way to get the macro name
regards
sounak
 
C

Chris Dollin

sounak said:
i need to get the macro name from the value
like suppose
#define a 100

if now i give input to a function as 100 then a should be
returned
just the opposite of the defination we see in macro

C does not support this. If you want to run the mapping backwards,
you have to do it yourself.
do we have to access the sysbol table

When the program runs, there is no symbol table [1].

If you want to do this - and /why/ do you want to do this? - you
can create your own table:

static struct { int value; char *name; } backwards =
{
{ A, "A" },
{ B, "B" },
// and so on, for all the macros you want to do it to
{ 0, 0 }
};

Now you can look up the value and find the corresponding string.

[1] Not one that can be portably accessed by an ANSI C program.
In any case, usually the #defines have gone before we have
what's usually called a "symbol table".
 
S

sounak

tell me one thing
while compiling the compiler replaces the values of the defined macros
after then the table is gone ?


now to get the values at run time
we can do one thing
parse the compiled file
and find out #define statement
and store the values in some structure as you told
and then show the values
Is it this thing you are saying?
sounak
 
P

pete

sounak said:
tell me one thing
while compiling the compiler replaces the values of the defined macros
after then the table is gone ?

now to get the values at run time
we can do one thing
parse the compiled file
and find out #define statement

There are no macros in a translation unit.
 
J

Jordan Abel

When the program runs, there is no symbol table [1].

[1] Not one that can be portably accessed by an ANSI C program.
In any case, usually the #defines have gone before we have
what's usually called a "symbol table".

Well, the preprocessor maintains its own construct that might reasonably
be called a "symbol table", but that's not even available non-portably
at execution time, let alone portably.
 
C

Chris Dollin

Jordan said:
When the program runs, there is no symbol table [1].

[1] Not one that can be portably accessed by an ANSI C program.
In any case, usually the #defines have gone before we have
what's usually called a "symbol table".

Well, the preprocessor maintains its own construct that might reasonably
be called a "symbol table",

It might, but I wouldn't, only because I expect a "symbol table" to
contain information about all the symbols in a translation unit,
and the #defines table thingy only holds the #defines and nothing
about eg the variables of the program. Hence the weasel.
 
M

mazsx

sounak said:
How could we get a macro name from a macro value
such that
in a header file
#define a 100
#define b 200
now the source file will be such that
the user gives 100
then the value is outputted as a
the user gives 200
then the value is outputted as b


is it possible
thanks in advance
sounak

As the folks explained, it is not possible in a portable way.
But I do think, that non-portable hooks exist.
Consider the following code under a GNU/Linux system.
(So this post might be off-topic. I would still like to refer to the
enum constants and the creation of symbol table entries that
my can be useful for the original questioner.)

/* contains non-portable code */
/* compilation (if called macroback.c)
* gcc -O -rdynamic -ansi -pedantic -Wall macroback.c -ldl -o
macroback
*/

#include <stdio.h>
#define concat(a,b) a##b
#define my_define(name,value) char *concat(const_id_,value)=#name; \
enum { name = value }


/* values should be non-negative integer constants */

my_define(A,12);
my_define(B,11);

const char **symbol_access(const char *name);

int main()
{
char array[A];
int i;
printf("A=%d works\n",sizeof array);
printf("B=%d works\n",B);
while(scanf("%d",&i)==1) {
char name[16];
const char **found;
sprintf(name,"const_id_%d",i);
if((found=symbol_access(name))) printf("%s=%d\n",*found,i);
else printf("Value %i not found\n",i);
}
return 0;
}

/* OS dependent part, should work under GNU linux */

#include <dlfcn.h>
const char **symbol_access(const char *symbol_name)
{
static void * handle;
if(handle || (handle=dlopen(NULL,RTLD_LAZY)))
return dlsym(handle,symbol_name);
fprintf(stderr,"Could not access symbol table: %s\n",dlerror());
return NULL;
}

mazsx
 
K

Keith Thompson

sounak said:
How could we get a macro name from a macro value
such that
in a header file
#define a 100
#define b 200
now the source file will be such that
the user gives 100
then the value is outputted as a
the user gives 200
then the value is outputted as b

The first thing you should do is learn how to post proper followups
(this is in reference to your other followups in this thread). Don't
assume that everyone can see the article to which you're replying.
You need to provide some context, as I've done here, so each article
can be read on its own. Google makes it gratuitously difficult to do
this, but there is a workaround (that's been posted here over 1000
times).

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

As for your question, the answer is that there's no way to do what
you're asking with macros, because that's not what macros are for.
You can build a table mapping strings to numbers, and look up and
display which strings (if any) are mapped to a specified number; it
just doesn't make any sense to use macro definitions for this purpose.

What are you really trying to accomplish, and why do you think that
macro definitions should be part of the solution?
 
T

Thad Smith

sounak said:
i need to get the macro name from the value
like suppose
#define a 100

if now i give input to a function as 100 then a should be
returned just the opposite of the defination we see in macro

do we have to access the sysbol table
if yes then how could we do that
and is there any other way to get the macro name

Normally, I would construct my own table, as Chris Dollin has
suggested, for the symbols that I want to use.

If you really want to automatically search from macros defined in C
source and don't want to construct your own table manually, as
suggested, you can write a parser to read the relevant C source,
extract the macro names and replacement text, place them into a table
containing both name and value, then compile that module and link into
your other code.

The relevant question is: why are you attempting to do this from
existing macro definitions, as opposed to constructing your code to
meet your project requirements?
 
M

Mike Wahler

Keith Thompson said:
The first thing you should do is learn how to post proper followups
(this is in reference to your other followups in this thread). Don't
assume that everyone can see the article to which you're replying.
You need to provide some context, as I've done here, so each article
can be read on its own. Google makes it gratuitously difficult to do
this, but there is a workaround (that's been posted here over 1000
times).

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

As for your question, the answer is that there's no way to do what
you're asking with macros, because that's not what macros are for.
You can build a table mapping strings to numbers, and look up and
display which strings (if any) are mapped to a specified number; it
just doesn't make any sense to use macro definitions for this purpose.

What are you really trying to accomplish, and why do you think that
macro definitions should be part of the solution?

I have done something like what OP is asking about when learning
Windows programming, to watch Windows messages and have their
macro names visible (e.g. 'WM_PAINT', 'BN_CLICKED', etc.).

I created a little utility that scanned the appropriate headers
and created a C source file containing tables which associated
the macro names to their values, and #included that in my application.

There are existing 'Spy' programs that can do this message watching
(one comes with VC++), I did mine so that I could integrate the 'spying'
with the application rather than switching between it and a separate 'spy'
program, and admittedly also for fun.

Later I added this table building to the application logic itself, making
it 'dynamic' (searching the headers and adding table entries as needed
during lookup).

Doing things like this does have an inherent 'fragility', as headers can
change (and/or change locations/availability) over time, so one must
ensure the availability (and correct version) of the headers, and make
sure the same header is used during compilation and execution. Then
there are the issues of macros inside #if/#endif etc. and those inside
comments. It would be quite a large chore to take all that into account,
I only went as far with that as needed to meet my immediate needs
(IOW "I cheated a bit" :) )

Elsethread folks warned about e.g. macros defined in terms of others
(that did happen a bit with my specific example), and different macros
producing the same values. So of course context must be considered (e.g.
in my example, many of the Windows control notification macros have the
same values, so lookup of the correct macro needs more context information
(such as a window class name).

Anyway, my point is that while it's 'uncommon', I don't think what OP
wants is completely unthinkable.

But as you say, we need OP to state a specific problem in order to
offer the most appropriate advice.

-Mike
 
M

Mark McIntyre

How could we get a macro name from a macro value

you can't - macros are literally replaced in the text of your code
with their value, before compilation. at runtime the macro no longer
exists.
#define a 100 ....
the user gives 100
then the value is outputted as a

You'd have to have an auxilliary array or two

char defnarray[1000] ;
defnarray[100] = 'a';
defnarray[200]='b';
etc

and lookup one into the other.

You may be able to write a C programme to parse your header and
automatically generate the array, so that you can compile it into
your other C programme.
 

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

Similar Threads


Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top