Jonathan Levin
Jonathan Levin

Reputation: 654

C macro functions - how to recognize a substring containing arg?

I'm trying to define the following macro functions:

#define TEST_INSTRUCTIONS_INPUT(name) const InstructionArray name =
#define TEST_INSTRUCTIONS_SIZE(name) const uint16_t name_size = sizeof(name) / sizeof(name[0]);

The first works, but the second does not (name is not replaced).

In general it seems F(x) x_name will not replace x, so F(test) -> x_name, rather than test_name

My guess is the C preprocessor:

Is there a way around this to get what I want? (Function declerations with "decorated" names).

Thanks.

Upvotes: 0

Views: 207

Answers (1)

KamilCuk
KamilCuk

Reputation: 141493

Is there a way around this to get what I want?

Yes, concatenate the tokens. This is what ## is for.

#define TEST_INSTRUCTIONS_SIZE(name) const uint16_t name##_size = ...

Upvotes: 1

Related Questions