It's pretty straight forward:
#define FOO(buf, str) \
strcpy(buf, str); \
write_strongswan_conf(buf, strlen(buf));
Note that the buffer size argument was superfluous, so it is not needed here.
Then refer to it via:
int main ()
{
char wr_buffer[1000];
FOO(wr_buffer, "STRONG SWAN CONFIGURATION FILE");
FOO(wr_buffer, "THIS IS THE SECOND LINE");
return(0);
}
If you really did need a local argument in your macro, you could define a block with a macro:
#define FOO(buf, str) \
{ \
int bsize; \
strcpy(buf, str); \
bsize = strlen(buf); \
write_strongswan_conf(buf, bsize); \
}
EDIT: Thanks to comments from @jweyrich and @BillLynch, they point out the potential pitfalls with the above method, and it is preferred to render a code block as follows when in a macro:
#define FOO(buf, str) \
do { \
strcpy(out, str); \
write_strongswan_conf(buf, strlen(str)); \
} while (0)
The block structure has one other benefit, and that is if you want to use your macro in an a compound statement in C. If you take the first definition above and do this:
if ( some_check )
FOO(wr_buffer, "STRONG SWAN CONFIGURATION FILE");
You wouldn't get the expected results since the if
would only apply to the first of the three statements. Of course, one good solution is to always block your statements:
if ( some_check ) {
FOO(wr_buffer, "STRONG SWAN CONFIGURATION FILE");
}
But you could also use the pattern do { ... } while (0)
in the macro definition as shown in the second example above, and that would avoid the issue as well.
Note that it is sometimes important to parenthesize arguments in macro definitions. I did not do so above since it isn't likely an issue, but let's suppose you have a macro such as this:
#define MUL(X, Y) (X * Y)
This looks innocent enough. Let's try it:
int a = 2;
int b = 3
int c;
c = MUL(a, b);
This generates:
c = (a * b);
Note that #define
s are quite literal. The parentheses are included. The preprocessor is just doing a direct substitution of everything I defined. If my expression is more complex:
c = MUL(a + 2, b + 3);
Then I get:
c = (a + 2 * b + 3);
Wait... this is going to give me a result which is equivalent to a + (2*b) + 3
which is probably not what I wanted. So I define the macro more carefully with parentheses:
#define MUL(X, Y) ((X) * (Y))
Now the above will expand to: c = ((a + 2) * (b + 3));
.
By the way, if you get any strange behavior when using macros and/or other #define
s, it can be handy to run just the preprocessor on your .c
file which will expand all of the #define
s without compilation. The output can be a bit unwieldy at first, but it's handy for identifying problems with macros that are complex. You can see exactly what it generated.
In Unix/Linux, you would just run the cpp
command: cpp myfile.c > myfile.pre
or whatever you want to call the file. If you're using Visual Studio, there's likely an option for generating the preprocessed file.
For the record, if it were me, I'd probably make this a function, not a macro. It certainly saves only a tiny percentage of execution timemaking something like this a macro.
As in all aspects of coding: it's important to think through and carefully about what you're doing. :)