473,406 Members | 2,273 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,406 software developers and data experts.

How to define a define that defines some defines ?

Hi all,

I have the following:

/*--- SNIP ---*/

typedef struct Argument_s
{
char *address;
int type;
int length;
} ARGUMENT;

#define Function(F) int F( int ArgCount, ARGUMENT ArgVector[] )

#define First ArgVector[0]
#define First_A First.address
#define First_T First.type
#define First_L First.length
#define Second ArgVector[1]
#define Second_A Second.address
#define Second_T Second.type
#define Second_L Second.length
#define Third ArgVector[2]
#define Third_A Third.address
#define Third_T Third.type
#define Third_L Third.length

Function(One)
{
/* do something with First_A */
/* do something with First_T */
/* do something with First_L */
/* do something with Second_A */
/* do something with Second_T */
/* do something with Second_L */
/* do something with Third_A */
/* do something with Third_T */
/* do something with Third_L */
/* etc etc */
}

#define Apple ArgVector[0]
#define Apple_A Apple.address
#define Apple_T Apple.type
#define Apple_L Apple.length
#define Orange ArgVector[1]
#define Orange_A Orange.address
#define Orange_T Orange.type
#define Orange_L Orange.length

Function(Two)
{
/* do something with Apple_A */
/* do something with Apple_T */
/* do something with Apple_L */
/* do something with Orange_A */
/* do something with Orange_T */
/* do something with Orange_L */
/* etc etc */
}

/*--- SNIP ---*/

The question is: Is there a way instead of writing all those #defines
for the arguments (First, Second, etc), to write something like:

#define Arg(n) #define Arg ArgVector[n] \
#define Arg##_A Arg.address \
#define Arg##_T Arg.type \
#define Arg##_L Arg.length

and define the arguments like this:

Arg First(0)
Arg Second(1)
Arg Third(2)
Function(One)
.....
Arg Apple(0)
Arg Orange(1)
Function(Two)
.....
Thanks in advance everyone.
Nov 14 '05 #1
3 10552
theotyflos <tk**@in.gr> scribbled the following:
typedef struct Argument_s
{
char *address;
int type;
int length;
} ARGUMENT; #define Function(F) int F( int ArgCount, ARGUMENT ArgVector[] ) #define First ArgVector[0]
#define First_A First.address
#define First_T First.type
#define First_L First.length
#define Second ArgVector[1]
#define Second_A Second.address
#define Second_T Second.type
#define Second_L Second.length
#define Third ArgVector[2]
#define Third_A Third.address
#define Third_T Third.type
#define Third_L Third.length #define Apple ArgVector[0]
#define Apple_A Apple.address
#define Apple_T Apple.type
#define Apple_L Apple.length
#define Orange ArgVector[1]
#define Orange_A Orange.address
#define Orange_T Orange.type
#define Orange_L Orange.length The question is: Is there a way instead of writing all those #defines
for the arguments (First, Second, etc), to write something like: #define Arg(n) #define Arg ArgVector[n] \
#define Arg##_A Arg.address \
#define Arg##_T Arg.type \
#define Arg##_L Arg.length and define the arguments like this: Arg First(0)
Arg Second(1)
Arg Third(2)
Function(One)
....
Arg Apple(0)
Arg Orange(1)
Function(Two)
....


No. The preprocessor is a one-pass utility. It is impossible for it to
#define its own directives.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
- Kent Brockman
Nov 14 '05 #2
theotyflos wrote:
Hi all,

I have the following:

/*--- SNIP ---*/

typedef struct Argument_s
{
char *address;
int type;
int length;
} ARGUMENT;

#define Function(F) int F( int ArgCount, ARGUMENT ArgVector[] )

#define First ArgVector[0]
#define First_A First.address
#define First_T First.type
#define First_L First.length
#define Second ArgVector[1]
#define Second_A Second.address
#define Second_T Second.type
#define Second_L Second.length
#define Third ArgVector[2]
#define Third_A Third.address
#define Third_T Third.type
#define Third_L Third.length

Function(One)
{
/* do something with First_A */
/* do something with First_T */
/* do something with First_L */
/* do something with Second_A */
/* do something with Second_T */
/* do something with Second_L */
/* do something with Third_A */
/* do something with Third_T */
/* do something with Third_L */
/* etc etc */
}

#define Apple ArgVector[0]
#define Apple_A Apple.address
#define Apple_T Apple.type
#define Apple_L Apple.length
#define Orange ArgVector[1]
#define Orange_A Orange.address
#define Orange_T Orange.type
#define Orange_L Orange.length

Function(Two)
{
/* do something with Apple_A */
/* do something with Apple_T */
/* do something with Apple_L */
/* do something with Orange_A */
/* do something with Orange_T */
/* do something with Orange_L */
/* etc etc */
}

/*--- SNIP ---*/

The question is: Is there a way instead of writing all those #defines
for the arguments (First, Second, etc), to write something like:

#define Arg(n) #define Arg ArgVector[n] \
#define Arg##_A Arg.address \
#define Arg##_T Arg.type \
#define Arg##_L Arg.length

and define the arguments like this:

Arg First(0)
Arg Second(1)
Arg Third(2)
Function(One)
....
Arg Apple(0)
Arg Orange(1)
Function(Two)
....
Thanks in advance everyone.


Looks like a better design is to have a function that
accesses the structures:
void Set_Arguments(struct Argument_s * arg,
char * address,
int type,
int length)
{
arg->address = address;
arg->type = type;
arg->length = length;
return;
}

One could "Take it up a notch" by using constant identifiers:
#if NO_ENUMERATIONS
#define APPLE 0
#define ORANGE 1
#define WATERMELON 2
#else
enum {APPLE, ORANGE, WATERMELON};
#endif

struct Argument_s fruits[5];

/* ... */
Set_Arguments(&fruit[APPLE], "hello", 1, 25);
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #3
In <c1**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
No. The preprocessor is a one-pass utility. It is impossible for it to
#define its own directives.


But it (usually) can be invoked twice, the second invocation processing
the output of the first one. However, I have yet to find a *good*
reason for this approach...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4

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

Similar topics

14
by: Rajan | last post by:
Hi All C++ Experts (1)I want have a simple suggestion from u all experts which is preferable const or #define and in which cases (2)in my project i want to avoid hardcoding , for that i have...
4
by: Vittal | last post by:
Hello All, Here is a small C program, main() { int a= 100; float b =99.99; TEST(a,%d); TEST(b,%f);
5
by: Kevin | last post by:
Hi every one. I want to use marco '#define' define a function like printf that have variable arguments, do it possible? it may be like that, I write a function foo(int LineNo, char * fmt, ...) I...
3
by: QQ | last post by:
How to define a string? Usually we can #define MAX 30 However if I'd like to define a string const can I? #define S "Hello"
6
by: David Young | last post by:
Hello all, I'm quite new to C# (< 6 months) but really love it and is my language of choice ..... but I have one question I've not been able to find out ..... In C++ a #define label in one...
2
by: #define中的问题 | last post by:
#define RGB(r,g,b)((COLORREF)((BYTE)(r)| \ ((WORD)((BYTE)(g))<<8))| \ ((DWORD)(BYTE)(b))<<16))) what is the "| \" mean above? who give me a answer? Thank you very mauch!
17
by: niraj.tiwari | last post by:
What is meaning of the following define:- #define x(argl...) x1(##argl)
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 \ ... \ ... \
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...
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: 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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.