sizeof() and preprocessor

Hello,

The following are quotes from K&R:

"A sizeof cannot be used in a #if line, because the preprocessor does not parse type names. But the expression in the #define is not evaluated by the preprocessor, so the code here is legal."

The expression referred to is

#define NKEYS (sizeof(keytab)/sizeof(struct key))

Here is the context:
struct key{
char* word;
int count;
};
struct key keytab[]= {/*initialization list*/};

To determine the number of elements in keytab, K&R use the #define statement. The following is the justification provided:
"....The size of the array is completely determined at compile time."

I have the following doubts:

1. Does not the preprocessing phase precede the compilation phase? If so, how is the #define statement not evaluated by the preprocessor?

2. If the type name is part of a token, should not the preprocessor parse the same? For instance,
#define array(x) int x[15]

array(values) = {/*initialization list*/};
Here, the typename is part of the replacement token. So after the preprocessing phase will the code look like
int values[15] = {/*initialization list*/};
or will it produce an error? If so, why?

3. In the same context, if I write
#define join(a, b) ab
and then
printf("%d", join(values, [15]));
Will the code read as below after the preprocessing phase,
printf("%d", values[15]);
or will it produce an error? If so, why?

Can any of the members help?

Thanks in Advance
PV




Comments

  • : Hello,
    :
    : The following are quotes from K&R:
    :
    : "A sizeof cannot be used in a #if line, because the preprocessor
    : does not parse type names. But the expression in the #define is not
    : evaluated by the preprocessor, so the code here is legal."
    :
    : The expression referred to is
    :
    : #define NKEYS (sizeof(keytab)/sizeof(struct key))
    :
    : Here is the context:
    : struct key{
    : char* word;
    : int count;
    : };
    : struct key keytab[]= {/*initialization list*/};
    :
    : To determine the number of elements in keytab, K&R use the #define
    : statement. The following is the justification provided:
    : "....The size of the array is completely determined at compile time."
    :
    : I have the following doubts:
    :
    : 1. Does not the preprocessing phase precede the compilation phase?
    : If so, how is the #define statement not evaluated by the
    : preprocessor?

    ige> Yes the preprocessing happens before the compilation.
    But the sizeof(...) opearator is evaluated at the runtime and not at compile-time.
    Whereever you will write "NKEYS" , it will be replaced by "(sizeof(keytab)/sizeof(struct key))" (without quotes obviously), before the actual compilation of the code happens.
    At runtime, this expression will be evaluated correctly.

    :
    : 2. If the type name is part of a token, should not the preprocessor
    : parse the same? For instance,
    : #define array(x) int x[15]
    :
    : array(values) = {/*initialization list*/};
    : Here, the typename is part of the replacement token. So after the
    : preprocessing phase will the code look like
    : int values[15] = {/*initialization list*/};
    : or will it produce an error? If so, why?

    ige> Yes it will look like that.

    :
    : 3. In the same context, if I write
    : #define join(a, b) ab
    : and then
    : printf("%d", join(values, [15]));
    : Will the code read as below after the preprocessing phase,
    : printf("%d", values[15]);
    : or will it produce an error? If so, why?

    ige> Yes this will give an error. The code after preprocessing will be:
    printf("%d", ab);
    And "ab" is not defined. (In case there is a variable called "ab", it will not give you a compilation error, but thats not what you wanted!)

    To concatenate the tokens (token pasting) , you will have to define it as

    #define join(a, b) a##b

    This will give you that result.

    This link might be helpful to you:
    http://www.cprogramming.com/tutorial/cpreprocessor.html

    :
    : Can any of the members help?
    :
    : Thanks in Advance
    : PV
    :
    :
    :
    :
    :

This discussion has been closed.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion