473,382 Members | 1,657 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,382 software developers and data experts.

#define macro

147 100+
Hi.
Expand|Select|Wrap|Line Numbers
  1. #define DEFINE_COMMAND(name, description, refRequired, numParams, paramInfo) \
  2.     kCommandInfo_name = { \
  3.     #name, \
  4.     "", \
  5.     0, \
  6.     #description, \
  7.     refRequired, \
  8.     numParams, \
  9.     paramInfo, \
  10.     HANDLER(Cmd_name_Execute), \
  11.     Cmd_Default_Parse, \
  12.     NULL, \
  13.     0 \
  14.     };
I want whatever string is passed as the name parameter to be substituted in two longer identifiers, i.e. if I pass Horticulture for name, I should get back kCommandInfo_Horticulture and Cmd_Horticulture_Execute.
Seems the preprocessor requires space before and after a token, of course.
The stringizing and token-pasting operators don't appear to help here.
Is there a way to get the preprocessor to insert a passed token into a longer identifier?
Thanks.
Apr 14 '08 #1
5 2799
Banfa
9,065 Expert Mod 8TB
You will probably need more macros so you can take advantage of the token pasting operator a bit like

Expand|Select|Wrap|Line Numbers
  1. #define CONCAT(a,b)  a ## b
  2. #define CONCAT3(a,b,c)  CONCAT(CONCAT(a,b),c)
  3.  
and then call these from your original #defined macro

P.S. No guarantees these work without modification as my daughter lunch is a higher priority task than running these through a compiler for you :D
Apr 14 '08 #2
scruggsy
147 100+
P.S. No guarantees these work without modification as my daughter lunch is a higher priority task than running these through a compiler for you :D
:D
Thanks for the tip. I'll fool around with it some more.
Apr 14 '08 #3
scruggsy
147 100+
Got it. Thanks again.
While on the subject, is it possible to view the code generated by a macro or must I assume that if it compiles the macro expanded correctly (which isn't always the case)?

Working macro:
Expand|Select|Wrap|Line Numbers
  1. #define DEFINE_COMMAND(name, description, refRequired, numParams, paramInfo) \
  2.     (kCommandInfo_ ## name) = { \
  3.     #name, \
  4.     "", \
  5.     0, \
  6.     #description, \
  7.     refRequired, \
  8.     numParams, \
  9.     paramInfo, \
  10.     HANDLER(Cmd_ ## name ## _Execute), \
  11.     Cmd_Default_Parse, \
  12.     NULL, \
  13.     0 \
  14.     };[
  15.  
  16. CommandInfo DEFINE_COMMAND(AnimPathIncludes, 
  17.                returns true if the actor is playing an anim containing the substring,
  18.                1,
  19.                1,
  20.                kParams_OneString);
  21.  
Correctly gives:
Expand|Select|Wrap|Line Numbers
  1. CommandInfo kCommandInfo_AnimPathIncludes =
  2. {
  3.     "AnimPathIncludes",
  4.     "",
  5.     0,
  6.     "returns true if the actor is playing an anim containing the substring",
  7.     1,
  8.     1,
  9.     kParams_OneString,
  10.     HANDLER(Cmd_AnimPathIncludes_Execute),
  11.     Cmd_Default_Parse,
  12.     NULL,
  13.  
  14. };
Apr 14 '08 #4
Banfa
9,065 Expert Mod 8TB
Many compilers (but not all) have a switch to output preprocessed files. This is the file after all preprocessing has happened (i.e. ALL # statements so #defines and macro expension, #includes etc).

Check your compiler documentation for a switch like this for your compiler (note in MSVC the switch for the compiler exists but I do not think it is available through the IDE you have to add it manually).

A preprocessed file will be long, probably with large blank portions (corresponding to a #if statement that is false, include a header with preprocessor inclusion protection twice and you will get the header followed by the number of blank lines equal to the length of the header).

A search and replace of 2 consecutive blank lines to 1 can help increase readability of it.

The preprocessed file is compilable and this can aid debugging since you step through the actual code as opposed to the preprocessor definitions.


I have worked with automatic code generators where the generated code contain about 10 levels of nested function like macros with none of the code looking like recognisable C or C functions due to the level of use of macros. When bugs existed in the code the only way to trace them was to compile to preprocessed source. Of course once you worked out where the bug in the preprocessed code was you had to try and work out which bit of which macro contained the error. Then of course contact the code generators maker and try and get the to change it.
Apr 14 '08 #5
scruggsy
147 100+
Wonderful. Thanks again.
Apr 14 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Robert | last post by:
Greetings everyone, I was wondering if a const variable or object took up space. I know that a #define'd macro doesn't, as it's basically just interpreted by the compiler. If a const does take...
9
by: pozz | last post by:
Hi all, I have the below #defines #define NUMBER1 30 #define NUMBER2 50 #define SUM (NUMBER1+NUMBER2) #define STRING1 "Byte: \x30" #define STRING2 "Byte: \x50"...
13
by: marcwentink | last post by:
Dear people, The code below is compiling: #define BUF_SZ 16383 ..... strcat(ConnectString, "Content-Length: BUF_SZ\n"); but it does not work since it give me:
29
by: Ancient_Hacker | last post by:
It sure would be nice if I could have a macro that add a level of indirection to its argument. So if I write: AddIndirection( X ) The macro AddIndirection will do: #define X (*X) ...
19
by: Sensei | last post by:
Hi! I'm concerned about the legality of such a definition: #define funcX funcY where funcX belongs to the *standard* C functions. Is it legal to do this? The standard says "any function...
4
by: Mohammad Omer Nasir | last post by:
I was read Linux kernel code in which I saw few define macro defines in functions. The snap of the function is following and file name "err_ev6.c". static int ev6_parse_cbox(u64 c_addr, u64...
6
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
14
by: raghu | last post by:
Hello I have a doubt plz clarify that #ifndef SYSTEM_H #define SYSTEM_H what will be the value of SYATEM_H after this #define statement and before that statement. Thanking you all Bye
4
by: venkat | last post by:
I have come across some preprossor statements, such as #define PPTR_int #define PPTR_str #define DDAR_baddr & #define DDAR_caddr & What do they mean, but when i compile the code with these...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.