I am trying to store a Preprocessor Constant's value then 'overwrite' it.
My Problem: The code below attempts to store a preprocessor constant's value in variable 'A' then the code undefines that variable then redefines it so it has a new value. The problem is that variable 'A' has the newly defined value and not the old one, if that makes sense. Can I store a Preprocessor Constants value and not its reference(Which is what seems like is happening)?
#ifdef CUSTOM_EVENT_CALLBACK_DEFINED
#define SUB_CUSTOM_EVENT_CALLBACK_DEFINED CUSTOM_EVENT_CALLBACK_DEFINED
#undef CUSTOM_EVENT_CALLBACK_DEFINED
#endif
#define CUSTOM_EVENT_CALLBACK_DEFINED "def"
int main()
{
printf(CUSTOM_EVENT_CALLBACK_DEFINED); // prints out "def".
printf("\n");
printf(SUB_CUSTOM_EVENT_CALLBACK_DEFINED); // prints out "def". I was hoping this would be "abc"
printf("\n");
system("PAUSE");
return 0;
}
// Usage: where the function def is a callback function for a window in a civil engineering program that runs ontop of windows
int def(int id, int cmd, int type)
{
#ifdef SUB_CUSTOM_EVENT_CALLBACK_DEFINED
SUB_CUSTOM_EVENT_CALLBACK_DEFINED(id, cmd, type);
#endif
// perform my custom code here
}
Short answer - no, it's not possible. Macros don't work like that.
But I doubt you really need to do this. A workaround, for example, would be to store the value in a variable before you overwrite it:
#ifdef CUSTOM_EVENT_CALLBACK_DEFINED
std::string SUB_CUSTOM_EVENT_CALLBACK_DEFINED = CUSTOM_EVENT_CALLBACK_DEFINED;
#undef CUSTOM_EVENT_CALLBACK_DEFINED
#else
std::string SUB_CUSTOM_EVENT_CALLBACK_DEFINED = "";
#endif
#define CUSTOM_EVENT_CALLBACK_DEFINED "def"
int main()
{
printf(CUSTOM_EVENT_CALLBACK_DEFINED); // prints out "def".
printf("\n");
printf(SUB_CUSTOM_EVENT_CALLBACK_DEFINED); // prints out "def". I was hoping this would be "abc"
printf("\n");
system("PAUSE");
return 0;
}
Or not use macros at all for this purpose.
Related
Suppose i have code like this in my program:
if (!strcmp(current, "sin")) {
pushFloat(sin(x), &operands);
} else if (!strcmp(current, "cos")) {
pushFloat(cos(x), &operands);
} else if (!strcmp(current, "tan")) {
pushFloat(tan(x), &operands);
} else if (!strcmp(current, "ctg")) {
pushFloat(1. / tan(x), &operands);
} else if (!strcmp(current, "ln")) {
pushFloat(log(x), &operands);
} else if (!strcmp(current, "sqrt")) {
pushFloat(sqrt(x), &operands);
}
There are function names such as "sin" or "cos" saved in the current char array
Instead of using this long if block, or replacing it with an even longer switch block, i wanted to write a simple macro like this: #define PUSHFUNC(stack, func, value)(pushFloat(func(value), &stack)) and call it like this PUSHFUNC(operands, current, x)
Doing it this way creates an error "current is not a function or function pointer". I initially thought macros are just text replacement, so if i force a string that is equal to an actual function into a macro, it would expand to the function itself, but looks like i was wrong. Is there a way to achieve what i want using a macro, or should i just write a map block?
I initially thought macros are just text replacement,
That's your problem: macros are just text replacement. So if you have:
#define PUSHFUNC(stack, func, value) (pushFloat(func(value), &stack))
And you write:
PUSHFUNC(operands, current, x)
You get:
(pushFloat(current(value), &operands))
And indeed, you have no function named current. Macros are expanded before your code compiles; the preprocessor has no knowledge of the content of your variables.
If you really want to avoid a long chain of if statements, you could implement some sort of table lookup:
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <math.h>
typedef double (*floatop)(double x);
typedef struct {
char *name;
floatop operation;
} entry;
double ctg(double);
entry opertable[] = {
{"sin", sin},
{"cos", cos},
{"tan", tan},
{"ctg", ctg},
{"sqrt", sqrt},
{NULL, NULL},
};
double ctg(double x) {
return 1. / tan(x);
}
floatop findop(char *name) {
int i;
for (i=0; opertable[i].name; i++) {
if (strcmp(opertable[i].name, name) == 0) {
return opertable[i].operation;
}
}
}
int main() {
float x = 4;
printf("sin(%f) = %f\n", x, findop("sin")(x));
printf("sqrt(%f) = %f\n", x, findop("sqrt")(x));
printf("tan(%f) = %f\n", x, findop("tan")(x));
printf("ctg(%f) = %f\n", x, findop("ctg")(x));
}
...but this requires that all of your functions take the same arguments, so for things like ctg you would need to add a helper function. You also need to decide if the increased complexity of the table lookup makes sense: it really depends on how many different operation names you expect to implement.
The output of the above code is:
sin(4.000000) = -0.756802
sqrt(4.000000) = 2.000000
tan(4.000000) = 1.157821
ctg(4.000000) = 0.863691
Is there a way to achieve what i want using a macro, or should i just write a map block?
I would recommend using an enum containing symbols for all the functions you might want to call, and using that in a switch-case block, instead of comparing a bunch of strings. Here's a very brief sample that only uses some of the functions you refer to...
enum which_func { SIN, COS, TAN, };
enum which_func which = SIN;
switch (which) {
case SIN:
pushFloat(sin(x), &operands);
break;
case COS:
pushFloat(cos(x), &operands);
break;
case TAN:
pushFloat(tan(x), &operands);
break;
default:
assert(false); // shouldn't be reachable if enum value is well-defined
}
This version will be easier to maintain in the long run, more efficient to execute and possibly more robust to logic errors (there are some compiler warnings that you can enable which will warn you if you're not handling all enum values, which can help you catch missed cases in your logic).
To add to what other answers said, what you can do is to make a macro that expands to the "basic block" of your if chain, avoiding some repetitions thanks to the stringizing operator:
#define HANDLE_FN_EXPR(fn, expr) \
else if(!strcmp(current, #fn)) \
pushFloat((expr), &operands)
#define HANDLE_FN(fn) \
HANDLE_FN_EXPR(fn, fn(x))
Then you can do
if(0);
HANDLE_FN(sin);
HANDLE_FN(cos);
HANDLE_FN(tan);
HANDLE_FN_EXPR(ctg, 1./tan(x));
HANDLE_FN(ln);
HANDLE_FN(sqrt);
Macros do in fact do text replacement. Given your macro definition, this:
PUSHFUNC(operands, current, x)
expands to this:
(pushFloat(current(x), &operands))
So as you can see, the text that is being replaced is the name of the variable, not the text that it contains.
And even if this did work as you expected, it wouldn't be able to properly handle the 1. / tan(x) case.
This means there isn't really a better way to do what you want.
Why not create some objects for each function type? I know, this is C not C++, but the idea will still work. First, create the function object type:-
typedef struct _Function
{
char *name;
float (*function) (float argument);
} Function;arg
And now create an array of function objects:-
Function functions [] =
{
{ "sin", sin },
{ "cos", cos }
// and so on
};
where the functions are defined:-
float sin(float x)
{
return 0; // put correct code here
}
float cos(float x)
{
return 0; // put correct code here
}
Finally, parse the input:-
for (int i = 0; i < sizeof functions / sizeof functions[0]; ++i)
{
if (strcmp(functions[i].name, current) == 0)
{
pushFloat(functions[i].function(arg)); // add operands!
break;
}
}
I find using enums for stuff like this very hard to maintain! Adding new functions means going through the code to find cases where the enum is used and updating it prone to errors (like missing a place!).
All because it's not C++, doesn't mean you can't use objects! It's just there's no language support for it so you have to do a bit more work (and, yeah, there are features missing!)
I define my exceptions on the command line:
-DEXCEPTION_1=\"first\" -DEXCEPTION_2=\"second\" -DEXCEPTION_3=\"third\"
which I check against a string:
except = 0;
#ifdef EXCEPTION_1
if (! strcmp(EXCEPTION_1, mystring))
{ except = 1;
}
#endif
#ifdef EXCEPTION_2
if (! strcmp(EXCEPTION_2, mystring))
{ except = 1;
}
#endif
#ifdef EXCEPTION_3
if (! strcmp(EXCEPTION_3, mystring))
{ except = 1;
}
#endif
if (except == 1)
{ // do something
}
else
{ // do something else
}
Needless to say, while this works, it is also quite ugly, inflexible and causes redundancy in my code.
Is there a way to append a string to a preprocessor macro variable?
I would like to get something like this (the problem of course is that #append does not exist):
#ifdef EXCEPTION_1 #append EXCEPTIONS if (! strcmp(EXCEPTION_1, mystring)) { except = 1; }
#ifdef EXCEPTION_2 #append EXCEPTIONS if (! strcmp(EXCEPTION_2, mystring)) { except = 1; }
#ifdef EXCEPTION_3 #append EXCEPTIONS if (! strcmp(EXCEPTION_3, mystring)) { except = 1; }
Then I could just use EXCEPTIONS in the code and it would work with all possible permutations of exceptions.
In other words I want to append a string to a macro variable - is it possible?
You can have chains of defines, but it won't look much better:
#ifdef EXCEPTION_1
#define EXCEPTIONS1 if (! strcmp(EXCEPTION_1, mystring)) { except = 1; }
#else
#define EXCEPTIONS1
#endif
#ifdef EXCEPTION_2
#define EXCEPTIONS2 EXCEPTIONS1 if (! strcmp(EXCEPTION_2, mystring)) { except = 1; }
#else
#define EXCEPTIONS2 EXCEPTIONS1
#endif
// etc
Again, not much better.
And you really shouldn't define macros with open if's. It allows weird interactions like if(cond) EXCEPTIONS1 else cout<<"error"; -- that won't do what you expect because EXCEPTIONS1 is a plain if and will gobble up the else branch.
The typical way of writing macros with code blocks is to wrap the whole thing in a do{...}while(0) (note no ending ;).
Checkout the token stringification and concatenation section here
It might help some. In general, it would seem easier to use your command line macro to populate a table and have a macro that simply expands to a loop that checks the successive entries of that table to set the except flag.
For instance, save the following as silly.c and compile with cc -DEX1=\"hello\"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct Except {
const char* key;
int flag;
};
struct Except table[] = {
#if defined(EX1)
{EX1,1},
#endif
{NULL,0}
};
#define CHECKEX(mys,rv) { rv = 0;for(int i=0;table[i].key!=0;i++) \
if (strcmp(table[i].key,(mys))==0) \
rv = 1; \
}
int main()
{
int rv;
CHECKEX("hello",rv);
if (rv)
printf("Got an hello\n");
else printf("Got nothing\n");
return 0;
}
Simply add more "blocks" to the table as needed. Just a suggestion of course.
You can convert a macro argument to a string:
#define STR(x) #x
STR(hello) --> "hello"
And you can concatenate string literals by just writing them next to each other:
"123" "abc" --> "123abc"
Or create a macro:
#define CONCAT(a, b) a b
CONCAT("hello", " world!") --> "hello world!"
You can use ## to concatenate string to macros. Use an index and use the conditions only to set the index. Then at the end you can concatenate the index with your macro EXCEPTION. like,
define a macro like #define append(name) name ## counter
this will in turn give you name as name1 if counter is 1
Now you can define different append macros to come with the result in a single variable name
Here's the thing:
Let's say I have two function defined in C:
test_1() {};
test_2() {};
I would like to have a macro (e.g. NUM_TEST) that will refer to test number. Best way is to show it in code:
#define NUM_TEST 1
test_1() {};
test_2() {};
int main() {
test_ ## NUM_TEST ## ()
}
I would appreciate, if someone would help, to find a solution, how to concat name of function with macro.
EDIT:
To make it more clear. I would like to just by changing of "macro NUM_TEST" change invoked function between test_1() and test_2().
Yes I know there are more easier ways to do that, but this is just an example to more general problem: How to concat macro with text in C without adding new lines or new macro functions.
EDIT 2:
Obviously I was now clear enough. Let's say I wrote a program. It has two (or more) run types. I have one macro called NUM_TEST. By setting mentioned macro to 1 or 2 a want to choose run type between test_1() or test_2()
Thank you!
Is this what you're looking for?
#include <stdio.h>
#define TEST(NUM) test_ ## NUM ()
test_1() { printf ("Hello "); }
test_2() { printf ("World!\n"); }
int main (void)
{
TEST(1);
TEST(2);
// Prints "Hello World!\n"
return 0;
}
Rather than creating a Macro that determines this, it is better to just pass command line argument to it which represents the test number. Like this:
#include <stdio.h>
int main( int argc, char *argv[] )
{
switch(argv[1])
{
case "1" :
Test_1();
break;
case "2" :
Test_2();
break;
default :
printf("Test ID not found");
}
}
If you are, however, just looking to alias a name, you can have your functions Test_1, Test_2, etc. Then just have a generic TESTTORUN wherever you want the selected test to run. On compilation have the preprocessor replace it with the function name you want:
#define TESTTORUN Test_1 //Or Test_2 or whatever
This will cause the compiler to replace TESTTORUN everywhere in the program with Test_1.
I want to pass values to the macro through for loop,but when i try to pass values it gives error, please help m as fast as possible. When values of i are passed to macro as Valve(i) it gives error
my code given below:
#define Valve(x) stTest.bValve##x##_Cmd
typedef struct OperationFlags
{
int bValve1_Cmd;
int bValve2_Cmd;
}FLAGS_TypeDef;
void main(void)
{
FLAGS_TypeDef stTest;
int j,i;
stTest.bValve1_Cmd = 4;
stTest.bValve2_Cmd = 9;
for(i=1;i<=2;i++)
{
j=Valve(1);
printf("%d",j);
}
}
It is normal!
The preprocessor (the "thing" that processes the macros) is run BEFORE the C compiler. So, it is only valid when it produces compilable code.
In your case, if you use the code you show
j=Valve(1)
it will work for that value, since it will produce:
j=stTest.bValve1_Cmd
but it will do the entire loop only with that value.
When you change the parameter "1" with the "i" for actually doing the loop, then it will produce:
j=stTest.bValvei_Cmd
which is invalid.
To do what you want, just use a vector:
typedef struct OperationFlags
{
int bValve_Cmd[2];
}FLAGS_TypeDef;
#define Valve(x) stTest.bValve_Cmd[x]
//....
for(i=1;i<=2;i++)
{
j=Valve(1);
printf("%d",j);
}
Macro replacement is done well before runtime, so you cannot use a variable X containing the value 2 to get stTest.bValve2_Cmd. Instead, you will get stTest.bValveX_Cmd, for which no symbol exists.
You will have to find another way of doing this, such as having an array of values for which you can use X to select:
#define Valve(x) stTest.bValveX_Cmd[x]
typedef struct OperationFlags {
int bValveX_Cmd[2];
} FLAGS_TypeDef;
try this #define Valve(x) (x == 1 ? stTest.bValve1_Cmd : stTest.bValve2_Cmd)
#define Valve(x) (*(&stTest.bValve1_Cmd + (x-1)))
note : It may not work if the environment changes. Also it can not be used in the bit field.
add check
#define Valve(x) (*(&stTest.bValve1_Cmd + (x-1))); \
assert(offsetof(FLAGS_TypeDef, bValve2_Cmd) == sizeof(int))
I would like to force a functions parameters to accept only specific definitions. For example, consider #define OUTPUT 1, #define INPUT 0 and void restrictedFunction(int parameter); .
How would I force restrictedFunction(int parameter) to accept only OUTPUT or INPUT?
I would also like to take into consideration that another definition may have the same value, for example, #define LEFT 1 and #define RIGHT 0.
So in this case I would like restrictedFunction(int parameter) to be able to accept only OUTPUT and INPUT specifically.
typedef enum { INPUT = 0, OUTPUT = 1 } IO_Type;
void restrictedFunction(IO_Type parameter) { ... }
It doesn't absolutely force the use of the values (the compiler will let someone write restrictedFunction(4)), but it is about as good as you'll get.
If you truly want to force the correct type, then:
typedef enum { INPUT = 0, OUTPUT = 1 } IO_Type;
typedef struct { IO_Type io_type } IO_Param;
void restrictedFunction(IO_Param parameter) { ... }
In C99 or later, you could call that with:
restrictedFunction((IO_Param){ INPUT });
This is a compound literal, creating a structure on the fly. It is not entirely clear that the structure type really buys you very much, but it will force the users to think a little and may improve the diagnostics from the compiler when they use it wrong (but they can probably use restrictedFunction((IO_Param){ 4 }); still).
What this means is that your restrictedFunction() code should be ready to validate the argument:
void restrictedFunction(IO_Type io_type)
{
switch (io_type)
{
case INPUT:
...do input handling...
break;
case OUTPUT:
...do output handling...
break;
default:
assert(io_type != INPUT && io_type != OUTPUT);
...or other error handling...
break;
}
}
You could use an enum.
typedef enum TrafficDirection { INPUT = 0, OUTPUT = 1 } TrafficDirection;
restrictedFunction(TrafficDirection direction);
of course, this isn't perfect. You can still pass any int to it as long as you use a cast.
restrictedFunction((TrafficDirection) 4);
You don't get quite as much protection as you might like, but you can do:
enum func_type { INPUT, OUTPUT };
void restrictedFunction( enum func_type parameter );
You can use a wrapper to validate the argument:
#define restrictedFunction(x) do { \
static_assert((x) == INPUT || (x) == OUTPUT); \
assert(!strcmp(#x, "INPUT") || !strcmp(#x, "OUTPUT")); \
restrictedFunction(x); \
} while(0)
Notes:
This assumes restrictedFunction() returns a void. If it returns a value which you actually use, you'll need something like gcc's compound statement http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html. Or--better--you can use BUILD_BUG_ON_ZERO (see What is ":-!!" in C code?), which I keep forgetting about, because it doesn't seem to work with C++.
The do ... while(0) is to "swallow the semi-colon"; not really relevant here.
static_assert() is a compile-time assert; there are many variants available. Here is a link to one, https://stackoverflow.com/a/9059896/318716, if you don't have your own handy.
assert() is the standard run-time assert.
With gcc 4.1.2, and my version of static_assert(), you can replace the run-time assert() with a compile-time assert when the two !strcmp()'s are replaced with ==; see example below. I haven't tested this with other compilers.
x is only used once in the macro expansion, since the first four references are only used at compile-time.
When your actually define your function, you'll have to add parentheses to disable the macro expansion, as in:
void (restrictedFunction)(int x){ ... }
Also, if your code has a special case (whose code doesn't?) where you need to call restrictedFunction() with the argument foo, you'll need to write:
(restrictedFunction)(foo);
Here is a complete example, which puts a wrapper around the standard library function exit():
#include <stdlib.h>
#define CONCAT_TOKENS(a, b) a ## b
#define EXPAND_THEN_CONCAT(a,b) CONCAT_TOKENS(a, b)
#define ASSERT(e) enum{EXPAND_THEN_CONCAT(ASSERT_line_,__LINE__) = 1/!!(e)}
#define ASSERTM(e,m) enum{EXPAND_THEN_CONCAT(m##_ASSERT_line_,__LINE__)=1/!!(e)}
#define exit(x) do { \
ASSERTM((x) == EXIT_SUCCESS || (x) == EXIT_FAILURE, value); \
ASSERTM(#x == "EXIT_SUCCESS" || #x == "EXIT_FAILURE", symbol); \
exit(x); \
} while(0)
int main(void) {
exit(EXIT_SUCCESS); // good
exit(EXIT_FAILURE); // good
exit(0); // bad
exit(3); // doubly bad
}
If I try to compile it, I get:
gcc foo.c -o foo
foo.c: In function 'main':
foo.c:17: error: enumerator value for 'symbol_ASSERT_line_17' is not an integer constant
foo.c:18: warning: division by zero
foo.c:18: error: enumerator value for 'value_ASSERT_line_18' is not an integer constant
foo.c:18: error: enumerator value for 'symbol_ASSERT_line_18' is not an integer constant