Home > OS >  What is the "(x)" term in macro
What is the "(x)" term in macro

Time:03-09

I found a structure definition as shown below,

#define MAX_SIZE(x) 1

struct var
{

    int temp;
    int temp2[MAX_SIZE(temp)];

};

What does MAX_SIZE(temp) mean?.

What is the dimension of temp2 array?.

CodePudding user response:

I suspect the intention here is to document which field of the structure governs the actual size of a flexible array member. Since C provides no way to tell the compiler the actual size of a flexible array member, it's intentional that MAX_SIZE ignores its argument, because the C compiler can't make any use of the information. It's just for human readers. (This should have been explained in documentation comments above the definition of MAX_SIZE.)

Whoever wrote the code is confusing the issue by using the really old notation for flexible array members: neither the C99 sanctioned syntax temp2[] nor the older GNU extension temp2[0], but the way you wrote it in the days of pcc, temp2[1]. Are you looking at the source code to a very old program? Is it still using old-style function definitions perchance?

Anyway, you should understand the definition of struct var as being

struct var
{
    int temp;
    int temp2[temp];
};

... in the counterfactual version of C in which you can actually write that, of course.

CodePudding user response:

If you are curious what the preprocessor is doing, your compiler can generate the preprocessed file for you. For example, compile this with gcc -E try.c -o try.i and take a look at the output.

This fragment is small enough you can just do gcc -E try.c, but in general if you include anything the preprocessed output can get quite long.

This macro is pretty useless: anything you give it resolves to 1

  • Related