1

I'm trying to define a C++ header file that accesses an array element defined in a macro.

The array is defined as:

#define NOZZLE_TO_PROBE_OFFSET { 27, 35, -1.5 }

I'm trying to access it like this, to obtain the first element:

#define Z_STEPPER_ALIGN_XY { {  NOZZLE_TO_PROBE_OFFSET[0] , Y_BED_SIZE/2 }, { X_BED_SIZE,  Y_BED_SIZE/2 } }

But I get the following error:

Marlin/src/gcode/calibrate/../../inc/../../Configuration_adv.h:659:57: error: expected '}' before '[' token
   #define Z_STEPPER_ALIGN_XY { {  NOZZLE_TO_PROBE_OFFSET[0] , Y_BED_SIZE/2 }, { X_BED_SIZE,  Y_BED_SIZE/2 } }
                                ~                        ^

I'm having trouble remembering my macro expansion rules and also can't seem to summon the right google terms to help on this. The message makes sense but I'm not sure what to try as an alternative to represent array access. I suppose what I want the preprocessor to do is embed the array literal followed by the access, so that the output would expand to something like { 27, 35, -1.5 }[0] I appreciate the feedback on this admittedly n00by question!

0

1 Answer 1

6
#define NOZZLE_TO_PROBE_OFFSET { 27, 35, -1.5 }

is not an array. The macro only does text replacement and { 27, 35, -1.5 } is at most the initializer list for an array, but probably whatever other grammar construct depending on the context in which it is used. In any case there is no array in it inherently. You cannot extract one element from it.

I suggest you replace all the macros with actual arrays/tuples/vectors, potentially const or constexpr qualified, e.g.:

auto NOZZLE_TO_PROBE_OFFSET = std::array<double, 3>{ 27., 35., -1.5 };

// or since C++17
auto NOZZLE_TO_PROBE_OFFSET = std::array{ 27., 35., -1.5 };

or whatever variant satisfies the specific requirements you have for NOZZLE_TO_PROBE_OFFSET.

Macros should mostly be avoided if there are other ways of solving a problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.