I wrote the following macro:
#define m[a,b] m.values[m.rows*(a)+(b)]
However gcc gives me this error:
error: missing whitespace after the macro name
What is wrong and how do I fix it?
You cannot use [ and ] as delimiters for macro arguments; you must use ( and ). Try this:
#define m(a,b) m.values[m.rows*(a)+(b)]
But note that defining the name of a macro as the name of an existing variable may be confusing. You should avoid shadowing names like this.
I'm not familiar with any C preprocessor syntax that uses square brackets. Change
#define m[a,b] m.values[m.rows*(a)+(b)]
to
#define m(a,b) m.values[m.rows*(a)+(b)]
And it should work.
You cannot have such a macro that will expand when you supply arguments in square brackets. Wherever you got the idea that macros are a smart text-substituting tool, it's just the other way round: macros are extremely obtuse and stupid text-substitution mechanism. What you're trying to do with a macro is absolutely unwarranted - just write a named function.
Related
I have a library I am porting from Windows to Linux, and I am required to make the minimal number of changes necessary.
There are some function calls in the library that I need to call variants of, with one less parameter and I am trying to use a macro to perform the substitutions.
The issue is that these calls are passing the address of a structure and I'm getting the error "error: "&" may not appear in macro parameter list"
For example, if the library has the following call:
foo(param1, ¶m2);
and I need to replace it with
foo_variant(¶m2);
I am adding a conditional compile at the top of the file as follows:
#if defined LINUX_VARIANT
#define foo(param1, ¶m2) foo_variant(¶m2)
#endif
Is there any way to "escape" the ampersand to avoid this error?
If not, I did see another post that mentioned that a pointer to the struct could be defined, and use the pointer instead of taking the structure's address, but I'd like to avoid that unless there is no other option.
Thanks ahead for any enlightenment!
Just forward the whole argument expression:
#define foo(param1, param2) foo_variant(param2)
Note that, due to limitations of the preprocessor, this macro will fail if any of the arguments contains commas.
Quentin's answer is correct, but to give a bit more background: the preprocessor will textually replace any foo(... with foo_variant(... and will also textually replace the parameters. If one parameter in the C source file has an ampersand, it will just see that as part of the parameter.
So the preprocessor cannot accept an ampersand in its definition, it can accept an ampersand in its expansion. With:
#if defined LINUX_VARIANT
#define foo(param1, param2) foo_variant(¶m2)
#endif
then
foo(p1, p2);
will be replaced by
foo_variant(&p2);
A bunch of code just got handed over to me and I got baffled by macros in the header. I could not understand what they are for:
#define WRAPPER_MACRO(symbol) symbol
#define ANOTHER_SYMBOL WRAPPER_MACRO(ANOTHER_SYMBOL)
#define PREFIXED_ANOTHER_SYMBOL WRAPPER_MACRO(PFX_ANOTHER_SYMBOL)
Why do this? What is the benefit?
Edit: This is not the actual verbatim code from my codebase but it has the same template. I just replaced macro names.
As #Michael said, we'll need to see the real macros to know for sure. But without them, I'm willing to take a few guesses that might help you out.
The macro nesting is probably a stringification thing. This bit of code is from a codebase I maintain:
// As per http://gcc.gnu.org/onlinedocs/cpp/Stringification.html:
// "If you want to stringify the result of expansion of a macro argument, you
// have to use two levels of macros."
#ifndef STRINGIFY
#define STRINGIFY(s) TOSTRING(s)
#define TOSTRING(s) #s
#endif
I'm also guessing your PREFIXED_ANOTHER_SYMBOL macro is doing something similar to this, using the # or ## preprocessor directives to prepend a certain symbol to whatever you feed the macro.
I'd like to shorten variable names, so instead of this :
FPData.Temps.T.Solar.Val
I'd like to use :
TEMP_Solar.Val
and define macro :
#define TEMP_ FPData.Temps.T.
But it works only if I put space in between :
TEMP_ Solar.Val
compiles ok, but I'd like to use this one
TEMP_Solar.Val
Possible? I know I could get around by using macro and arguments "TEMP_VAL(Solar)" but would like to keep it simple, linear concatenation...
It's because the preprocessor, which handles macros, only recognizes their own identifiers. When you use e.g. TEMP_Solar it's a different identifier from TEMP_.
The preprocessor might even use a simple strcmp to find its macros, so there can't be no sub-strings nor can there be differences in case.
The most obvious and easy solution:
#define TEMP FPData.Temps.T
TEMP.Solar.Val
(You cannot and should not change the actual variable names of the struct members.)
I am trying to compare to a defined constants in C, and I have simplified my program to the following:
#include "stdio.h"
#include "stdlib.h"
#define INVALID_VALUE -999;
int main(void)
{
int test=0;
if(test==INVALID_VALUE) //The error line..
return INVALID_VALUE;
return 0;
}
And when I use gcc to compile, it gives out error "error: expected ‘)’ before ‘;’ token".
Is there any reason that this cannot be done?
Remove the semicolon from your INVALID_VALUE definition.
Macros are replaced lexically (character-by-character) with no understanding of the syntax around them. Your macro INVALID_VALUE is set to -999;, so your if line expands the macro to:
if (test==-999;)
which is invalid C syntax.
You need to remove the ; in #define INVALID_VALUE -999;. See the fixed code.
You could have worked towards this conclusion by understanding what the error message expected ‘)’ before ‘;’ token was telling you. It's telling you that it expected to find a ) before the ; token, but from inspecting the line alone you don't see a ;. So maybe there's one in the definition of INVALID_VALUE? Look up at #define INVALID_VALUE -999; and there it is! Think it should be there, but not sure? So let's try remove it and see if it works. Success!
This page goes and explains why you shouldn't conclude a #define with a semicolon, even if it is needed in the use of the macro. It's good to learn as much as possible from your mistake so that you don't make it again. Quote:
Macro definitions, regardless of
whether they expand to a single or
multiple statements should not
conclude with a semicolon. If
required, the semicolon should be
included following the macro
expansion. Inadvertently inserting a
semicolon at the end of the macro
definition can unexpectedly change the
control flow of the program.
Another way to avoid this problem is
to prefer inline or static functions
over function-like macros.
In general, the programmer should
ensure that there is no semicolon at
the end of a macro definition. The
responsibility for having a semicolon
where needed during the use of such a
macro should be delegated to the
person invoking the macro.
The C Preprocessor Macro Language is Distinct from C
The ; in the macro definition should be removed.
This is an understandable mistake. If C were designed today, the macro language might be more integrated with the rest of C.
But on 16-bit machines in the early 1970's when C was invented, it was unwise to write an overly complicated program. It would end up useless as there would be no memory remaining to actually run the big masterpiece program, and even simple programs ran slowly.
So C was split into a rather simple macro preprocessor that was, originally, a completely separate program, and the compiler proper. The preprocessor program made no attempt to parse C beyond understanding the lexical analysis model.
When 32-bit machines took over, the preprocessor was typically integrated into the parser, but naturally the language needed to remain the same.
The semi colon at the end of
#define INVALID_VALUE -999;
Classic.
You do not need a semicolon after defining something. #define is actually a macro, and it will do an inline expansion on compile.
Thus,
#define IDENTIFIER 10;
int j = IDENTIFIER;
will expand as:
int j = 10;;
Change macro
from:
#define INVALID_VALUE -999;
to
#define INVALID_VALUE -999
Bye
What is the proper way to perform a divide and round to lower number for a macro?
I am trying to do this:
#define TOTAL_NUM_FFTS (int) NO_SAMPLES / FFT_SIZE
but I am getting a warning of incompaitible redefinition of that macro and the compiler restates the line as:
#define TOTAL_NUM_FFTS(int) NO_SAMPLES / FFT_SIZE without the space between TOTAL_NUM_FFTS and (int).
Thanks for your help!
#define TOTAL_NUM_FFTS ((int) (NO_SAMPLES) / (FFT_SIZE))
The preprocessor thinks (int) is a parameter to the macro.
When defining macros, use as many parentheses as you can. For example, think what will happen if someone defines FFT_SIZE as 2+3. Instead of dividing by 5, you'd be dividing by 2 and then adding 3.
Several things to check:
always properly parenthesize your macros (and macro arguments), as Ilya mentioned
make sure there isn't a duplicate (or near duplicate) definition of the macro somewhere else. The error message should tell you exactly where, but if it doesn't, grep or similar will help (maybe there's an older version of your header hiding in some other directory f the include path?).
make sure your header file is protected against multiple inclusion with include guards. I don't think this is what's happening to you since since identical macro redefinition is supposed to be accepted by a C or C++ compiler, but you should still make sure your header has this.
For dividing, you can use the pow(base, exponent) function available in math.h library.
I suppose you have to implement something like,
#define C A/B //B!=0
then instead of it use the following:
#define C A*pow(B,-1)