That’d only work when you declare and define at the same time. Here’s a link with some more syntax. Note that it’s very C-flavor specific, so what may work for one specification (i.e. PROS/C99 may not work with PROS/C90, let alone ROBOTC).
I honestly don’t know how @tabor473 plans to “fully simulate that exact format” (I would love to find out how), but my suggestion is to do as follows:
//blah blah blah TYPICAL COMPETITION TEMPLATE STUFF, etc.
#define WHEEL_SIZE driveSettings.wheelSize =
#define WHEEL_BASE_WIDTH driveSettings.wheelBaseWidth =
#define THRESHOLD_ONE driveSettings.thresholdOne
#define THRESHOLD_TWO driveSettings.thresholdTwo
typedef struct {
char wheelSize;
int wheelBaseWidth;
short thresholdOne;
long thresholdTwo;
} drive_t;
drive_t driveSettings;
void pre_auton(){
WHEEL_SIZE 1; WHEEL_BASE_WIDTH 2; THRESHOLD_ONE = 3; THRESHOLD_TWO = 4;
//blah blah blah REST OF YOUR CODE, etc.
I used 2 slightly different formats:
WHEEL_SIZE
and
WHEEL_BASE_WIDTH
had equal signs in the
#define
statements, while
THRESHOLD_ONE
and
THRESHOLD_TWO
needed equal signs added in usage; you can choose the format that you prefer.
I know this wasn’t quite what you wanted, but this is the most user-friendly way to accomplish the task that I can think of. You can also hide away everything from (inclusive) my first comment to
drive_t driveSettings;
in an
#include
file if you so desire.
In most cases, I find that
#define
can be thought of as just a copy-and-paste system; for example,
driveSettings.wheelSize =
is pasted in place of
WHEEL_SIZE
automatically at compile time. More info on C preprocessors here
Thanks, I’d never thought of having a trailing equals, that’s really neat I may or may not use your ideas because you’re basically changing one variable name for another, seems kind of pointless.
for all the essential / basic settings (it also sets reasonable defaults). The user (which to be honest will probably just be me) can set any fancier settings manually (
MAKE_DRIVE sets the variables in the struct whose name it is passed. I had thought I could put the variable declaration in the define as well but it would be scoped in the do while statement.
What’s the advantage in your opinion of using preprocessor macros vs. a function? It looks the same to me. It accomplishes the same thing in the same number of instructions with any reasonable optimization.