DoJo
DoJo

Reputation: 55

Using macro with variables in c

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?

Upvotes: 1

Views: 103

Answers (1)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181270

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

Upvotes: 4

Related Questions