I can't figure out what this macro means:
#define DECLARE_HANDLE(n) typedef struct n##__{int i;}*n
DECLARE_HANDLE(HWND);
I have learned from the C program that:
"##" means connect the parameter.
so the macro equals:
typedef struct HWND__{int i;}*HWND
Is this right?
If it is right, what is the meaning of that sentence?
==================
Code from a game Bombermaaan (for Windows and Linux),
link http://sourceforge.net/p/bombermaaan/code/HEAD/tree/trunk/src/Bombermaaan/winreplace.h,
line No 90.
A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive. Here's an example. Here, when we use c in our program, it is replaced with 299792458 .
The Concept of C Macros Macros are generally used to define constant values that are being used repeatedly in program. Macros can even accept arguments and such macros are known as function-like macros. It can be useful if tokens are concatenated into code to simplify some complex declarations.
To invoke a macro that takes arguments, you write the name of the macro followed by a list of actual arguments in parentheses, separated by commas. The invocation of the macro need not be restricted to a single logical line—it can cross as many lines in the source file as you wish.
Macros are used to make a sequence of computing instructions available to the programmer as a single program statement, making the programming task less tedious and less error-prone. (Thus, they are called "macros" because a "big" block of code can be expanded from a "small" sequence of characters.)
Your assumption is correct.
You can check this with the following simple test code.
DECLARE_HANDLE(HWND);
struct HWND__ s;
HWND p = (HWND) malloc(sizeof(struct HWND__));
s.i = 20;
p->i = 100;
cout << "i valueis " << s.i<<" and " << p->i <<endl;
DECLARE_HANDLE(HWND);
indeed expands to
typedef struct HWND__{int i;}*HWND;
Now, you're asking what it means? Just split the typedef into two steps:
struct HWND__
{
int i;
};
typedef HWND__* HWND; // C++ only; for both C++ and C: typedef struct HWND__* HWND;
Thus, it defines a struct HWND__
containing an int
(named i
) and declares a type alias HWND
as pointer to HWND__
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With