LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2006, 12:27 AM   #1
Deepak Inbasekaran
Member
 
Registered: Apr 2006
Location: India
Distribution: Red Hat Linux release 9 (Shrike)
Posts: 44

Rep: Reputation: 15
reversing a macro


i have a macro defined as
Quote:
#define CARD 1
now if i want to do the reverse , is there any options like if i get "1" as input it must be printed as "CARD"

(any idea, other than using arrays )
 
Old 07-20-2006, 06:17 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
What you you do if you have:

Code:
#define CARD 1
#define KEY 1
AFAIK as I know there is not simple way to do it. I faced this problam once as I had a few hundred of these values defined, and I wanted the user to be able to use these names in a config file, and recongnize them in my program.

Since these macros are only known by the compiler I had to maintain a list of name/value combinations. For this I wrote a little parser to parse thru the appropriate files and find the combinations.

If you don't want to use an array, try a linked list, I think that was what I did.

jlinkels
 
Old 07-20-2006, 11:34 AM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
No you can't. This is not the way macros were meant to be used.
 
Old 07-20-2006, 01:11 PM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by Deepak Inbasekaran
i have a macro defined as


now if i want to do the reverse , is there any options like if i get "1" as input it must be printed as "CARD"

(any idea, other than using arrays )
other than arrays?

switch, or if
 
Old 07-20-2006, 01:18 PM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
i guess if it was c++ you could use STL, for example std::map..
 
Old 07-20-2006, 09:54 PM   #6
Deepak Inbasekaran
Member
 
Registered: Apr 2006
Location: India
Distribution: Red Hat Linux release 9 (Shrike)
Posts: 44

Original Poster
Rep: Reputation: 15
i got this solution and its working fine

Quote:
#include <stdio.h>
#define str(s) #s

#define LINE 1

int main()
{
printf("%s", str(LINE));
}
 
Old 07-21-2006, 12:41 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
So what does your solution actually do?

there are two macros defined, the first defines a function called str() and the other defines an identifier called LINE.

The preprocessor will then modify your code according to the defined macros, it will see the str macro and replace it, giving you:

Code:
#include <stdio.h>

int main()
{
printf("%s", "LINE");
}
There is no occurence of the identifier LINE for the preprocessor to modify and so it will be ignored. As others have said you are going to have to build a switch (for example) to actually reverse the macro.

I'm not entirely sure why you want to do that. There is probably a better way to address the problem, and I don't understand why you don't want to use an array, again that is because I don't understand what (at the end of the day) it is you are trying to achieve.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
#defining a macro inside a macro? saravkrish Programming 1 05-24-2005 09:48 PM
reversing program greenthing Programming 3 02-27-2005 08:08 PM
java, reversing digit Lifesaver Programming 5 03-03-2004 04:09 PM
reversing a number narendra_i Linux - Software 1 11-15-2003 09:25 AM
reversing videocard lackluster Linux - Hardware 1 11-16-2002 02:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration