1

I have a flat C file including ctype.h where i cant figure out how a macro works. There is this macro

#define da_dim(name, type)  type *name = NULL;          \
                            int _qy_ ## name ## _p = 0;  \
                            int _qy_ ## name ## _max = 0

I thought it should define the type of a given value. So for example i could write

int a;
da_dim(a,"char");

to convert it to a char but it doesnt do that. I can imagine what '## name ##' is/does (like a placeholder) but i dont understand what 'qy' is and where it came from. So what is this macro for, how tu use it and (maybe) how does it work?

5
  • I suggest you run the code through the preprocessor to see what the preprocessor replaces the macro invocation with (or attempt to do the replacement yourself on paper). Commented Dec 31, 2018 at 16:59
  • As for what the macro is for, have you asked the author? Or read the documentation of the code you saw the macro in? Commented Dec 31, 2018 at 17:00
  • gcc.gnu.org/onlinedocs/cpp/Concatenation.html Commented Dec 31, 2018 at 17:01
  • Lastly, your example usage of the macro is wrong and would lead to build errors. If you expand the macro (either on paper or using the preprocessor) you would see why. Commented Dec 31, 2018 at 17:01
  • I know its wrong, thats why i am asking this question. Commented Dec 31, 2018 at 19:10

1 Answer 1

4

A macro, in C is just a simple token replacement mechanism.

Your example:

int a;
da_dim(a,"char");

Will expand to:

int a;
"char" *a = NULL;
int _qy_a_p = 0;
int _qy_a_max = 0;

So, if will expand to errors because you will have two a identifiers and "char" is not expected where you are placing it.

If you are using gcc, you can "see" macro expansions by doing:

$ gcc -E your_program.c
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much i understand it :)
Thanks @EricPostpischil. Fixed that.

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.