473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

to get macro name from macro value

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

Nov 23 '05 #1
17 7025
Le 22-11-2005, sounak <so*****@gmail.com> a écrit*:
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
Nov 23 '05 #2
Marc Boyer <Ma********@enseeiht.yahoo.fr.invalid> wrote:
Le 22-11-2005, sounak <so*****@gmail.com> a écrit*:
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


....and the user enters 350?

Richard
Nov 23 '05 #3
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

Nov 23 '05 #4
Le 22-11-2005, sounak <so*****@gmail.com> a écrit*:
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
Nov 23 '05 #5
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

Nov 23 '05 #6
sounak wrote:
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".

--
Chris "another virtual machine" Dollin
missing tests don't fail.
Nov 23 '05 #7
if 2 macros have same value then 2 name will be shown

Nov 23 '05 #8
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

Nov 23 '05 #9
sounak wrote:

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.

--
pete
Nov 23 '05 #10
Chris Dollin wrote:
static struct { int value; char *name; } backwards =


Oops. s/backwards/backwards[]/.

--
Chris "another virtual machine" Dollin
missing tests don't fail.
Nov 23 '05 #11
On 2005-11-22, Chris Dollin <ke**@hpl.hp.com> wrote:

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.
Nov 23 '05 #12
Jordan Abel wrote:
On 2005-11-22, Chris Dollin <ke**@hpl.hp.com> wrote:

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.

--
Chris "pop11 goes the weasel" Dollin
missing tests don't fail.
Nov 23 '05 #13
sounak wrote:
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

Nov 23 '05 #14
"sounak" <so*****@gmail.com> writes:
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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 23 '05 #15
sounak wrote:

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?

--
Thad
Nov 23 '05 #16

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"sounak" <so*****@gmail.com> writes:
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?


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
Nov 23 '05 #17
On 22 Nov 2005 01:13:35 -0800, in comp.lang.c , "sounak"
<so*****@gmail.com> wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 23 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
1
by: meganrobertson22 | last post by:
Hi Everyone- I am trying to use a simple macro to set the value of a combo box on a form, and I can't get it to work. I have a macro with 2 actions: OpenForm and SetValue. I can open my form,...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
0
by: Chris Ellis | last post by:
Hi, I'm trying to write a macro that will generate managed c++ wrappers of map. I want a managed iterator and a managed map. I successfully built a pair of classes (iterator and map) that work...
28
by: Jack Morgan | last post by:
I got a macro like this in my code. > #define DECLARE_SOMEWNDSTRUCTURE(style, nums, clrref, icon, tablenum, name, menuname)\ static SOMEWNDSTRUCT &GetSomeWndStruct()\ {\ static SOMEWNDSTRUCT...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
1
by: Catbkr1 | last post by:
I have to automatically create some Excel Spreadsheets based on automatically generated .CSV files that are produced overnight. Each .CSV has several columns that need to be deleted. The same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.